-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1020-4.cpp
82 lines (71 loc) · 1.37 KB
/
A1020-4.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 35;
struct node{
int data;
node* l;
node* r;
};
int n;
int pre[maxn],mid[maxn],post[maxn];
void init(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&post[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&mid[i]);
}
}
node* create(int midl,int midr,int postl,int postr){
if(postl > postr) return NULL;
int k;
for(k=midl;k<=midr;k++){
if(mid[k] == post[postr]) break;
}
//指针变量一定要提前分配好空间
node* root = new node;
root->data = post[postr];
int numleft = k - midl;
root->l = create(midl,k-1,postl,postl+numleft-1);
root->r = create(k+1,midr,postl+numleft,postr-1);
// root->l = create(midl,k-1,postl,k-1);
// root->r = create(k+1,midr,k,postr-1);
return root;
}
int cts =0;
void layerTraverse(node* root){
queue<node*> q;
q.push(root);
while(!q.empty()){
node* temp = q.front();
q.pop();
cts++;
printf("%d",temp->data);
if(cts != n){
printf(" ") ;
}
if(temp->l) q.push(temp->l);
if(temp->r) q.push(temp->r);
}
}
void PreTraverse(node* root){
if(root != NULL){
printf("%d ",root->data);
PreTraverse(root->l);
PreTraverse(root->r);
}
}
int main(){
init();
node* root = create(0,n-1,0,n-1);
// cout<<"!";
// PreTraverse(root);
layerTraverse(root);
return 0;
}