Skip to content

Commit a65025a

Browse files
authored
Create main.cpp
1 parent 0fd73ff commit a65025a

File tree

1 file changed

+38
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/19 - Bottom View of Binary Tree

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
vector <int> bottomView(Node *root) {
4+
5+
vector<int> ans;
6+
if(root == NULL) return ans;
7+
8+
map<int, int> topNode;
9+
queue<pair<Node*, int>> q;
10+
11+
q.push({root, 0});
12+
13+
while(!q.empty()){
14+
pair<Node*, int> temp = q.front();
15+
q.pop();
16+
17+
Node* frontNode = temp.first;
18+
int hd = temp.second;
19+
20+
topNode[hd] = frontNode -> data;
21+
22+
if(frontNode -> left){
23+
q.push({frontNode -> left, hd -1});
24+
}
25+
26+
if(frontNode -> right){
27+
q.push({frontNode -> right, hd + 1});
28+
}
29+
30+
}
31+
for(auto i : topNode){
32+
ans.push_back(i.second);
33+
}
34+
35+
36+
return ans;
37+
}
38+
};

0 commit comments

Comments
 (0)