Skip to content

Commit f38420e

Browse files
committed
199. Binary Tree Right Side View
1 parent bfa73f1 commit f38420e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

binary-tree-right-side-view.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//Runtime: 3 ms
2+
class Solution {
3+
public:
4+
vector<int> rightSideView(TreeNode* root) {
5+
queue<pair<int, TreeNode* > >Q;
6+
Q.push(make_pair(1, root));
7+
vector<int>res;
8+
if(!root)
9+
return res;
10+
int maxlvl = 1;
11+
int prev = root->val;
12+
while(!Q.empty())
13+
{
14+
pair<int,TreeNode*> t = Q.front();
15+
Q.pop();
16+
17+
if(t.first > maxlvl)
18+
{
19+
res.push_back(prev);
20+
maxlvl++;
21+
}
22+
prev = t.second->val;
23+
if(t.second->left)
24+
Q.push(make_pair(t.first+1, t.second->left));
25+
26+
if(t.second->right)
27+
Q.push(make_pair(t.first+1, t.second->right));
28+
}
29+
30+
res.push_back(prev);
31+
32+
return res;
33+
}
34+
35+
};

0 commit comments

Comments
 (0)