-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day-93
41 lines (29 loc) · 967 Bytes
/
Day-93
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
vector<int> leftView(Node *root)
{
// Your code here
if(!root)
return {};
typedef pair<Node *, int> pi;
queue<pi> qu;
vector<int> ans;
int currLevel = 0, prevLevel = -1;
qu.push({root, 0}); // root is at 0 level
while(!qu.empty()){
pi temp = qu.front();
qu.pop();
currLevel = temp.second;
// if level of two consecutive nodes are different
// it means the node having higher level is the
// first in its level and should be inserted in ans.
if(currLevel != prevLevel){
ans.push_back(temp.first->data);
prevLevel = currLevel;
}
// level of children is parent+1
if(temp.first->left)
qu.push({temp.first->left, temp.second + 1});
if(temp.first->right)
qu.push({temp.first->right, temp.second + 1});
}
return ans;
}