-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopView.cpp
More file actions
28 lines (28 loc) · 761 Bytes
/
topView.cpp
File metadata and controls
28 lines (28 loc) · 761 Bytes
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
class Solution {
public:
vector<int> topView(Node *root) {
queue<pair<Node*,int>>q;
map<int,int>mpp;
vector<int>ans;
q.push({root,0});
while(!q.empty()){
auto it = q.front();
q.pop();
Node*temp = it.first;
int line = it.second;
if(mpp.find(line)==mpp.end()){
mpp[line] = temp->data;
}
if(temp->left){
q.push({temp->left,line-1});
}
if(temp->right){
q.push({temp->right,line+1});
}
}
for(auto it:mpp){
ans.push_back(it.second);
}
return ans;
}
};