-
Notifications
You must be signed in to change notification settings - Fork 0
/
1138.cpp
69 lines (52 loc) · 1.27 KB
/
1138.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
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
const int MAXN = 30 + 10;
struct Node {
int id;
Node* lc, *rc;
Node(int id) : id(id) {
lc = rc = nullptr;
}
void postOrder(bool &flag) {
if (lc) lc->postOrder(flag);
if (rc) rc->postOrder(flag);
if (flag) {
// ans.push_back(this->id);
printf("%d\n", this->id);
flag = false;
}
}
};
vector<int> prd, inrd;
//l1 r1->prd l2 r2->inrd
Node* build(int l1, int r1, int l2, int r2) {
if (l1 > r1) return nullptr;
Node *root = new Node(prd[l1]);
int tmp = l2;
while (inrd[tmp] != root->id) tmp++;
int num = tmp - l2;
root->lc = build(l1 + 1, l1 + num, l2, tmp - 1);
root->rc = build(l1 + num + 1, r1, tmp + 1, r2);
return root;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
prd.push_back(x);
}
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
inrd.push_back(x);
}
Node *root = build(0, n - 1, 0, n - 1);
bool flag = true;
root->postOrder(flag);
return 0;
}