-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
binaryTreeRightSideView.cpp
76 lines (71 loc) · 2.2 KB
/
binaryTreeRightSideView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Source : https://leetcode.com/problems/binary-tree-right-side-view/
// Author : Hao Chen
// Date : 2015-04-07
/**********************************************************************************
*
* Given a binary tree, imagine yourself standing on the right side of it, return
* the values of the nodes you can see ordered from top to bottom.
*
* For example:
* Given the following binary tree,
*
* 1 <---
* / \
* 2 3 <---
* \ \
* 5 4 <---
*
* You should return [1, 3, 4].
*
* Credits:Special thanks to @amrsaqr for adding this problem and creating all test cases.
*
**********************************************************************************/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void rightSideViewHelper(TreeNode* root, int level, vector<int>& result) {
if (root == NULL) return;
if ( result.size() < level ) result.push_back(root->val);
rightSideViewHelper(root->right, level+1, result);
rightSideViewHelper(root->left, level+1, result);
}
void rightSideViewHelper(TreeNode* root, vector<int>& result) {
if (root==NULL) return;
vector<TreeNode*> stack;
vector<int> level;
stack.push_back(root);
level.push_back(1);
while (stack.size()>0) {
TreeNode* r = stack.back(); stack.pop_back();
int l = level.back(); level.pop_back();
if ( result.size() < l ) {
result.push_back(r->val);
}
if (r->left) {
stack.push_back(r->left);
level.push_back(l+1);
}
if (r->right) {
stack.push_back(r->right);
level.push_back(l+1);
}
}
}
vector<int> rightSideView(TreeNode *root) {
vector<int> result;
if (rand()%2){
rightSideViewHelper(root, 1, result);
}else{
rightSideViewHelper(root, result);
}
return result;
}
};