-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
39 lines (38 loc) · 1016 Bytes
/
solution.cpp
File metadata and controls
39 lines (38 loc) · 1016 Bytes
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int val_arr[100] = {0,};
void f(TreeNode* node, int depth){
if(node == NULL) return;
val_arr[depth] = node -> val;
f(node -> left, depth + 1);
f(node -> right, depth + 1);
}
public:
vector<int> rightSideView(TreeNode* root) {
for (int i=0; i<100; i++) {
val_arr[i] = -101;
}
f(root, 0);
vector <int> result;
for (int i=0; i<100; i++) {
if (val_arr[i] >= -100) {
result.push_back(val_arr[i]);
}
else {
break;
}
}
return result;
}
};