-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathtree-top-view.cpp
More file actions
98 lines (83 loc) · 1.68 KB
/
Copy pathtree-top-view.cpp
File metadata and controls
98 lines (83 loc) · 1.68 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#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* create(int ino[], int pre[], int s, int e){
static int i=0;
if(s>e)
return NULL;
node* root = new node(pre[i]);
int index =1;
for(int j=s;j<=e;j++)
{
if(ino[j]==pre[i])
{
index = j;
break;
}
}
i++;
root->left = create(ino,pre,s,index-1);
root->right = create(ino,pre,index+1,e);
return root;
}
void insert(node* &root, int key)
{
queue<node*> q;
if(root==NULL)
{
root = new node(key);
return;
}
q.push(root);
while (!q.empty()) {
node* root = q.front();
q.pop();
if (!root->left) {
if(key!=-1)
root->left = new node(key);
else
root->left = NULL;
break;
} else
q.push(root->left);
if (!root->right) {
if(key!=-1)
root->right = new node(key);
else
root->right = NULL;
break;
} else
q.push(root->right);
}
}
void printtop(node* root){
if(root==NULL)
return;
printtop(root->left);
cout<<root->data<<" ";
}
void printtop2(node* root){
if(root==NULL)
return;
cout<<root->data<<" ";
printtop(root->right);
}
int main(){
int d;
node* root = NULL;
while(scanf("%d",&d)!=EOF)
{
insert(root,d);
}
printtop(root);
printtop2(root->right);
}