-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathA1086.cpp
58 lines (54 loc) · 1.04 KB
/
A1086.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
#include <cstdio>
#include <stack>
using namespace std;
const int MAXN = 40;
int pre[MAXN], in[MAXN], N, preN = 0, inN = 0;
stack<int> st;
struct Node{
int key;
Node *lchild, *rchild;
};
Node *buildTree(int preL, int preR, int inL, int inR){
if(preL > preR) return NULL;
Node *root = new Node;
root->key = pre[preL];
int k;
for(k = inL; k <= inR; ++k){
if(pre[preL] == in[k]){
break;
}
}
int numLeft = k - inL;
root->lchild = buildTree(preL+1, preL+numLeft, inL, k-1);
root->rchild = buildTree(preL+numLeft+1, preR, k+1, inR);
return root;
}
int cnt = 0;
void postOrder(Node *root){
if(root == NULL) return;
postOrder(root->lchild);
postOrder(root->rchild);
printf("%d", root->key);
++cnt;
if(cnt < N) printf(" ");
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N*2; ++i){
char op[8];
int x;
scanf("%s", op);
if(op[1] == 'u'){
scanf("%d", &x);
st.push(x);
pre[preN++] = x;
}else{
int top = st.top();
st.pop();
in[inN++] = top;
}
}
Node *root = buildTree(0, N-1, 0, N-1);
postOrder(root);
return 0;
}