-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeInorderPreorder.cpp
More file actions
77 lines (53 loc) · 1.09 KB
/
TreeInorderPreorder.cpp
File metadata and controls
77 lines (53 loc) · 1.09 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
#include<bits/stdc++.h>
using namespace std ;
class node{
public : int data;
node *left;
node *right ;
node()
{
}
node(int data)
{
left=NULL;
right=NULL;
this->data=data;
}
};
int find_index(int in[],int start,int end,int data)
{
while(start<=end)
{
if(in[start]==data)
return start ;
start++ ;
}
return 0;
}
node* make_tree(int pre[],int in[],int start,int end,int preindex)
{
if(start>end)
{
return NULL;
}
node *root=new node(pre[preindex]) ;
int index=find_index(in,start,end,pre[preindex] ) ;
root->left =make_tree(pre,in,start,index-1,preindex+1 ) ;
root->right =make_tree(pre,in,index+1,end , preindex+index-start+1 ) ;
return root ;
}
void printT(node *root)
{
if(root==NULL)
return ;
printT(root->left) ;
cout<<(root->data)<<" " ;
printT(root->right) ;
}
int main()
{a
int pre[]={1,2,3,4,5,6,7};
int in[]={3,2,5,4,1,7,6};
node* root=make_tree(pre,in,0,6,0);
printT(root) ;
}