-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTreeRightSideView.cpp
86 lines (67 loc) · 1.52 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
77
78
79
80
81
82
83
84
85
86
/*
* Definition for a b
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x): val(x), left(NULL), right(NULL) {}
} ;
class Solution {
public:
void Helper( TreeNode * root, vector<int> & result, int cur, vector<int> & marked ){
if(root == NULL) return;
/*
if( marked.size() == cur ){
marked.push_back(1);
result.push_back(root->val);
}
*/
if ( result.size() == cur) result.push_back(root->val);
Helper(root->right, result , cur+1, marked);
Helper(root->left , result , cur+1, marked);
}
vector<int> rightSideView(TreeNode * root){
vector<int> result, marked;
if(root == NULL) return result;
Helper(root, result, 0, marked);
return result;
}
};
int main(){
cout<<"starting"<<endl;
TreeNode root(1);
TreeNode a(2);
root.left = &a;
TreeNode b(3);
root.right = &b;
TreeNode c(5);
a.right = &c;
TreeNode d(4);
b.left = &d;
TreeNode e(7);
d.right = &e;
TreeNode f(6);
c.left = &f;
TreeNode g(9);
f.left = &g;
TreeNode i(10);
f.right= &i;
TreeNode j(11);
g.left=&j;
Solution sol;
vector<int> result = sol.rightSideView(&root);
// vector<int> result = sol.rightSideView(NULL);
for( int i = 0 ; i < result.size() ; i++){
cout<<result[i]<<endl;
}
return 0;
}