-
Notifications
You must be signed in to change notification settings - Fork 2
/
A1043.cpp
80 lines (73 loc) · 1.62 KB
/
A1043.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
#include <cstdio>
#include <vector>
using namespace std;
int N;
struct Node{
int data;
Node *lchild, *rchild;
};
vector<int> origin, pre, preM, post, postM;
void insert(Node *&root, int data){
if(root == NULL){
root = new Node;
root->data = data;
root->lchild = root->rchild = NULL;
return;
}
if(data < root->data) insert(root->lchild, data);
else insert(root->rchild, data);
}
void preOrder(Node *root, vector<int> &vi){
if(root == NULL) return;
vi.push_back(root->data);
preOrder(root->lchild, vi);
preOrder(root->rchild, vi);
}
void preOrderMirror(Node *root, vector<int> &vi){
if(root == NULL) return;
vi.push_back(root->data);
preOrderMirror(root->rchild, vi);
preOrderMirror(root->lchild, vi);
}
void postOrder(Node *root, vector<int> &vi){
if(root == NULL) return;
postOrder(root->lchild, vi);
postOrder(root->rchild, vi);
vi.push_back(root->data);
}
void postOrderMirror(Node *root, vector<int> &vi){
if(root == NULL) return;
postOrderMirror(root->rchild, vi);
postOrderMirror(root->lchild, vi);
vi.push_back(root->data);
}
int main(){
scanf("%d", &N);
Node *root = NULL;
for(int i = 0; i < N; ++i){
int data;
scanf("%d", &data);
origin.push_back(data);
insert(root, data);
}
preOrder(root, pre);
preOrderMirror(root, preM);
postOrder(root, post);
postOrderMirror(root, postM);
if(origin == pre){
printf("YES\n");
for(int i = 0; i < post.size(); ++i){
if(i != 0) printf(" ");
printf("%d", post[i]);
}
}else if(origin == preM){
printf("YES\n");
for(int i = 0; i < postM.size(); ++i){
if(i != 0) printf(" ");
printf("%d", postM[i]);
}
}else{
printf("NO");
}
return 0;
}