-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_Tree_Right_Side_View.cpp
54 lines (46 loc) · 1.34 KB
/
Binary_Tree_Right_Side_View.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
PROBLEM STATEMENT :- Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
Input: root = [1,null,3]
Output: [1,3]
Input: root = []
Output: []
SOLUTION :-
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root)
{
vector<int> ans;
if(root==NULL)
return ans;
queue<TreeNode *> q;
q.push(root);
while(q.empty()==false)
{
int cnt = q.size();
for(int i=0;i<cnt;i++)
{
TreeNode *curr = q.front();
q.pop();
if(i == cnt-1)
ans.push_back(curr->val);
if(curr->left)
q.push(curr->left);
if(curr->right)
q.push(curr->right);
}
}
return ans;
}
};