forked from Harry-003/Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree-bottom view
69 lines (62 loc) · 1.04 KB
/
Tree-bottom view
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
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
data=d;
left=right=NULL;
}
};
node *createlevelorder()
{
int d;
cin>>d;
node *root=new node(d);
queue<node*>q;
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 vop(node *root,int level,int d,map<int,pair<int,int> >&m)
{
if(root==NULL)
{
return;
}
if(m.find(d)==m.end() or level>=m[d].second)
m[d] = {root->data,level};
vop(root->left,level+1,d-1,m);
vop(root->right,level+1,d+1,m);
}
int main()
{
node *root=createlevelorder();
map<int,pair<int,int> >m;
vop(root,0,0,m);
for(auto it:m)
{
cout<<it.second.first<<" ";
}
return 0;
}