-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha.cpp
More file actions
62 lines (61 loc) · 1.15 KB
/
a.cpp
File metadata and controls
62 lines (61 loc) · 1.15 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
#include <bits/stdc++.h>
using namespace std;
typedef struct bstnode
{
bstnode *lc;
char data;
bstnode *rc;
} * bstptr;
bstptr z;
void insert(bstptr &t)
{
char k;
cin >> k;
if (k == '#')
return;
else
{
t = new bstnode();
t->data = k;
t->lc = NULL;
t->rc = NULL;
insert(t->lc);
insert(t->rc);
}
}
char hdarr[1000];
int lvl[100];
void topview(bstptr z, int hd, int &mina, int &maxa)
{
if (z == NULL)
return;
if (hd < mina || hd > maxa)
{
hdarr[hd] = z->data;
}
mina = min(mina, hd);
maxa = max(maxa, hd);
topview(z->lc, hd - 1, mina, maxa);
topview(z->rc, hd + 1, mina, maxa);
}
/* void topview(bstptr z,int hd,int &mina,int &maxa,int level){
if(z==NULL)return;
if(level>=lvl[hd]){
lvl[hd]=level;
hdarr[hd]=z->data;
}
mina=min(mina,hd);maxa=max(maxa,hd);
topview(z->lc,hd-1,mina,maxa,level+1);
topview(z->rc,hd+1,mina,maxa,level+1);
} */
int main()
{
insert(z);
int min = 99;
int max = -99;
topview(z, 0, min, max);
for (int i = min; i <= max; i++)
{
cout << hdarr[i] << " ";
}
}