We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0fd73ff commit a65025aCopy full SHA for a65025a
17 - Binary Tree Data Structure Problems/19 - Bottom View of Binary Tree/main.cpp
@@ -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