-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBottom_view.cpp
61 lines (44 loc) · 1.08 KB
/
Bottom_view.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
54
55
56
57
58
59
60
61
#include<iostream>
#include<map>
#include<vector>
#include<queue>
using namespace std;
class Node
{
public:
int data;
Node* left;
Node* right;
Node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
vector <int> bottomView(Node *root) {
vector<int> ans;
map<int,int> mapping;
queue< pair< Node* , int> > q;
q.push(make_pair(root, 0));
while( !q.empty())
{
pair<Node*, int> p = q.front();
q.pop();
int distance = p.second;
Node* newNode = p.first;
mapping[distance] = newNode->data;
if(newNode->left)
q.push( make_pair(newNode->left, distance -1 ));
if(newNode->right)
q.push(make_pair(newNode->right, distance + 1));
}
for(auto i:mapping)
ans.push_back(i.second);
return ans;
}
int main()
{
// question link -> https://practice.geeksforgeeks.org/problems/bottom-view-of-binary-tree/1
return 0;
}