forked from itsprueba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbottom_view.cpp
More file actions
72 lines (67 loc) · 1 KB
/
bottom_view.cpp
File metadata and controls
72 lines (67 loc) · 1 KB
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
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<queue>
#include<map>
using namespace std;
class node{
public:
int data;
int h_dist;
node *left;
node *right;
node(int d){
data=d;
left = NULL;
right = NULL;
}
};
node* buildTree(){
int d;
cin>>d;
queue<node*> q;
node *root = new node(d);
q.push(root);
while(!q.empty()){
node * f = q.front();
q.pop();
int c1,c2;
cin>>c1>>c2;
if(c1!=-1){
f->left = new node(c1);
q.push(f->left);
}
if(c2!=-1){
f->right = new node(c2);
q.push(f->right);
}
}
return root;
}
void bottom_view(node *root){
queue<node *> q;
map<int, int> m;
root->h_dist = 0;
q.push(root);
int h_dist;
while(!q.empty()){
node * f= q.front();
h_dist = f->h_dist;
q.pop();
m[h_dist]=f->data;
if(f->left){
f->left->h_dist = h_dist -1;
q.push(f->left);
}
if(f->right){
f->right->h_dist = h_dist +1;
q.push(f->right);
}
}
for(auto x:m){
cout<<x.second<<" ";
}
}
int main() {
node *root = buildTree();
bottom_view(root);
return 0;
}