Skip to content

Commit 383aa87

Browse files
Merge pull request #165 from shivamsingh67/patch-4
Create Binary Tree Right Side View.cpp
2 parents 451d8d6 + 57216ab commit 383aa87

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
public:
14+
vector<int> rightSideView(TreeNode* root) {
15+
vector<int>ans;
16+
if(!root){
17+
return ans;
18+
}
19+
queue<TreeNode*>q;
20+
21+
q.push(root);
22+
23+
while(!q.empty()){
24+
int n = q.size();
25+
TreeNode* tmp;
26+
for(int i = 0; i < n; i++){
27+
tmp = q.front();
28+
q.pop();
29+
if(tmp->left) q.push(tmp->left);
30+
if(tmp->right) q.push(tmp->right);
31+
}
32+
33+
ans.push_back(tmp->val);
34+
}
35+
return ans;
36+
}
37+
};

0 commit comments

Comments
 (0)