forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0199.cpp
More file actions
66 lines (47 loc) · 1.4 KB
/
0199.cpp
File metadata and controls
66 lines (47 loc) · 1.4 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//Solution 1 :- (DFS)
class Solution {
public:
void rightDfs(TreeNode* root, vector<int>& result, int depth){
if(!root)
return;
if(depth == result.size())
result.push_back(root -> val);
rightDfs(root -> right, result, depth + 1);
rightDfs(root -> left, result, depth + 1);
}
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
rightDfs(root, result, 0);
return result;
}
};
//Solution 2 :- (BFS)
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
if(!root)
return result;
queue<TreeNode*> q;
q.push(root);
q.push(NULL);
TreeNode* frnt;
while(!q.empty()){
if(q.front()){
frnt = q.front();
if(frnt -> left)
q.push(frnt -> left);
if(frnt -> right)
q.push(frnt -> right);
q.pop();
}
else{
result.push_back(frnt -> val);
q.pop();
if(!q.empty())
q.push(NULL);
}
}
return result;
}
};