We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bfa73f1 commit f38420eCopy full SHA for f38420e
binary-tree-right-side-view.cpp
@@ -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
31
32
33
34
35
+};
0 commit comments