Skip to content

Commit ecd0d39

Browse files
authored
Merge pull request #85 from InflixOP/patch1
Create #199. Binary Tree Right Side View.cpp
2 parents 5563083 + 9b0a30b commit ecd0d39

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

#199. Binary Tree Right Side View.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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> ans;
15+
vector<int> levelOrder(TreeNode* root,vector<int> &ans) {
16+
queue<TreeNode*> q;
17+
vector<int> a;
18+
q.push(root);
19+
q.push(NULL);
20+
while(!q.empty()){
21+
TreeNode* temp=q.front();
22+
q.pop();
23+
if(temp==NULL){
24+
reverse(a.begin(),a.end());
25+
ans.push_back(a[0]);
26+
a.clear();
27+
if(!q.empty()){
28+
q.push(NULL);
29+
}
30+
}
31+
else{
32+
a.push_back(temp->val);
33+
if(temp->left) q.push(temp->left);
34+
if(temp->right) q.push(temp->right);
35+
}
36+
}
37+
return ans;
38+
}
39+
vector<int> rightSideView(TreeNode* root) {
40+
if(root==NULL){
41+
return ans;
42+
}
43+
levelOrder(root,ans);
44+
return ans;
45+
}
46+
};

0 commit comments

Comments
 (0)