-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightViewBinaryTree.cpp
More file actions
40 lines (38 loc) · 1.01 KB
/
Copy pathRightViewBinaryTree.cpp
File metadata and controls
40 lines (38 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue <TreeNode*> q;
q.push(root);
vector <int> v;
while(root)
{
int n = q.size();
for(int i = 1 ; i <= n ; i++)
{
TreeNode* temp = q.front();
q.pop();
if(i==n)
v.push_back(temp->val);
if(temp->left!=NULL)
q.push(temp->left);
if(temp->right!=NULL)
q.push(temp->right);
}
if(q.size()==0)
break;
}
return v;
}
};
//https://leetcode.com/problems/binary-tree-right-side-view/submissions/
//refs
//https://www.geeksforgeeks.org/right-view-binary-tree-using-queue/