Skip to content

Commit 5e456e1

Browse files
authored
Create main.cpp
1 parent f9c7cea commit 5e456e1

File tree

1 file changed

+31
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/22 - Binary Tree Right Side View

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
private:
14+
void solve(TreeNode* root, vector<int> & ans, int level){
15+
if(root == NULL) return;
16+
17+
if(level == ans.size()) ans.push_back(root -> val);
18+
19+
solve(root -> right, ans, level+1);
20+
solve(root -> left, ans, level+1);
21+
22+
}
23+
public:
24+
vector<int> rightSideView(TreeNode* root) {
25+
vector<int> ans;
26+
27+
solve(root, ans, 0);
28+
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)