-
Notifications
You must be signed in to change notification settings - Fork 2
/
前序后序构造二叉树.cpp
59 lines (48 loc) · 940 Bytes
/
前序后序构造二叉树.cpp
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
#include<cstdio>
#include<sstream>
#define maxn 1000
int mid_order[maxn], post_order[maxn];
struct node{
node * left;
node * right;
int v;
node(int m):left(NULL), right(NULL), v(m){}
};
node *root=NULL;
int num;
char buf[maxn];
bool read_input(int *arr){
if(fgets(buf, maxn, stdin)==NULL) return false;
num = 0;
std::stringstream ss(buf);
int v;
while(ss>>v){
arr[num++] = v;
}
return true;
}
void build(int s1, int e1, int s2, int e2, node * &root){
if(s1 > e1) return;
int v = post_order[e2];
if(root==NULL){
root = new node(v);
}
int p = s1;
while(mid_order[p]!=v) p++;
int n = p - s1;
build(s1, p-1, s2, s2+n-1, root->left);
build(p+1, e1, s2+n, e2-1, root->right);
}
void dfs(node *root){
if(!root) return;
printf("%d ", root->v);
dfs(root->left);
dfs(root->right);
}
int main(){
while(read_input(mid_order)){
read_input(post_order);
build(0, num-1, 0, num-1, root);
dfs(root);
}
}