-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree_LeftSideView.cpp
53 lines (43 loc) · 1.13 KB
/
BinaryTree_LeftSideView.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
class Solution{
public:
vector<int> leftSideView(TreeNode* root){
vector<int> ans;
helper(root, 0, ans);
return ans;
}
void helper(TreeNode* root,int level,vector<int> &ans){
if(root==NULL)
return ;
if(ans.size()==level)
ans.push_back(root->val);
helper(root->left, level+1, ans);
helper(root->right, level+1, ans);
}
};
/// left side view using level order
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
if(!root)
return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int n=q.size();
for(int i=0;i<n;i++)
{
TreeNode * node = q.front();
q.pop();
if(node->right)
q.push(node->right);
if(node->left)
q.push(node->left);
if(i==n-1)
res.push_back(node->val);
}
}
return res;
}
};