-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy path1127. ZigZagging on a Tree (30) .cpp
49 lines (49 loc) · 1.44 KB
/
1127. ZigZagging on a Tree (30) .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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> in, post, result[35];
int n, tree[35][2], root;
struct node {
int index, depth;
};
void dfs(int &index, int inLeft, int inRight, int postLeft, int postRight) {
if (inLeft > inRight) return;
index = postRight;
int i = 0;
while (in[i] != post[postRight]) i++;
dfs(tree[index][0], inLeft, i - 1, postLeft, postLeft + (i - inLeft) - 1);
dfs(tree[index][1], i + 1, inRight, postLeft + (i - inLeft), postRight - 1);
}
void bfs() {
queue<node> q;
q.push(node{root, 0});
while (!q.empty()) {
node temp = q.front();
q.pop();
result[temp.depth].push_back(post[temp.index]);
if (tree[temp.index][0] != 0)
q.push(node{tree[temp.index][0], temp.depth + 1});
if (tree[temp.index][1] != 0)
q.push(node{tree[temp.index][1], temp.depth + 1});
}
}
int main() {
cin >> n;
in.resize(n + 1), post.resize(n + 1);
for (int i = 1; i <= n; i++) cin >> in[i];
for (int i = 1; i <= n; i++) cin >> post[i];
dfs(root, 1, n, 1, n);
bfs();
printf("%d", result[0][0]);
for (int i = 1; i < 35; i++) {
if (i % 2 == 1) {
for (int j = 0; j < result[i].size(); j++)
printf(" %d", result[i][j]);
} else {
for (int j = result[i].size() - 1; j >= 0; j--)
printf(" %d", result[i][j]);
}
}
return 0;
}