-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1119-2.cpp
62 lines (58 loc) · 1.21 KB
/
A1119-2.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
#include <cstdio>
using namespace std;
const int MAXN = 35;
int pre[MAXN], in[MAXN], post[MAXN], unique = 1, N;
struct Node{
int data;
Node *lchild, *rchild;
Node(int _d): data(_d), lchild(NULL), rchild(NULL) {}
};
Node *creat(int preL, int preR, int postL, int postR){
if(preL > preR) return NULL;
else if(preL == preR){
Node *root = new Node(pre[preL]);
return root;
}
Node *root = new Node(pre[preL]);
int k;
for(k = preL+1; k <= preR; ++k){
if(pre[k] == post[postR-1]){
break;
}
}
int numLeft = k - preL - 1;
if(numLeft > 0){
root->lchild = creat(preL+1, k-1, postL, postL+numLeft-1);
root->rchild = creat(k, preR, postL+numLeft, postR-1);
}else{
unique = 0;
root->lchild = NULL;
root->rchild = creat(k, preR, postL+numLeft, postR-1);
}
return root;
}
int cnt = 0;
void inOrder(Node *root){
if(root == NULL) return;
inOrder(root->lchild);
printf("%d", root->data);
if(cnt < N-1){
++cnt;
printf(" ");
}
inOrder(root->rchild);
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%d", &pre[i]);
}
for(int i = 0; i < N; ++i){
scanf("%d", &post[i]);
}
Node *root = creat(0, N-1, 0, N-1);
printf("%s\n", unique ? "Yes" : "No");
inOrder(root);
printf("\n");
return 0;
}