-
Notifications
You must be signed in to change notification settings - Fork 82
/
Binary Tree Right Side View.cpp
64 lines (55 loc) · 1.36 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
55
56
57
58
59
60
61
62
63
64
class Solution
{
public:
vector<int> rightSideView(TreeNode* root) {
if(root==nullptr){
vector<int>y;
return y;
}
vector<int>v;
queue<TreeNode*>q;
q.push(root);
while(!q.empty()){
int s=q.size();
int u;
while(s-->0){
TreeNode*q2=q.front();
int t=q2->val;
q.pop();
u=t;
if(q2->left){
q.push(q2->left);
}
if(q2->right){
q.push(q2->right);
}
}
v.push_back(u);
}
return v;
}
};
******************************************************************************************************************************************************
class Solution
{
public:
unordered_map<int, int> map;
void rightView(TreeNode *root, int level)
{
if (root == nullptr)
return;
map[level] = root->val;
rightView(root->left, level + 1);
rightView(root->right, level + 1);
}
vector<int> rightSideView(TreeNode *root)
{
vector<int> output;
rightView(root, 0);
for (int i = 0; i < map.size(); i++)
{
output.push_back(map[i]);
}
return output;
}
};