-
Notifications
You must be signed in to change notification settings - Fork 0
/
1102.cpp
94 lines (75 loc) · 1.94 KB
/
1102.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
#include <queue>
const int MAXN = 10 + 5;
struct Node {
int id;
Node *fa;
Node *lc, *rc;
Node() {
id = 0;
fa = lc = rc = nullptr;
}
void addChild(int type, Node *ch) {
ch->fa = this;
if (type == 1) lc = ch;
else if (type == 2) rc = ch;
}
void reverse() {
std::swap(lc, rc);
if (lc) lc->reverse();
if (rc) rc->reverse();
}
void inOrder(std::vector<int> &v) {
if (lc) lc->inOrder(v);
v.push_back(id);
if (rc) rc->inOrder(v);
}
} nodes[MAXN];
void bfs(Node *root, std::vector<int> &v) {
std::queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node *tmp = q.front();
q.pop();
v.push_back(tmp->id);
if (tmp->lc) q.push(tmp->lc);
if (tmp->rc) q.push(tmp->rc);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) nodes[i].id = i;
getchar();
for (int i = 0; i < n; i++) {
char lc, rc;
scanf("%c %c", &lc, &rc);
getchar();
if (isdigit(lc)) nodes[i].addChild(1, nodes + (lc - '0'));
if (isdigit(rc)) nodes[i].addChild(2, nodes + (rc - '0'));
}
Node *root = nullptr;
for (int i = 0; i < n; i++) {
if (!nodes[i].fa) {
root = &nodes[i];
break;
}
}
root->reverse();
std::vector<int> inord;
root->inOrder(inord);
std::vector<int> leord;
bfs(root, leord);
for (int i = 0; i < leord.size(); i++) {
if (i != leord.size() - 1) printf("%d ", leord[i]);
else printf("%d\n", leord[i]);
}
for (int i = 0; i < inord.size(); i++) {
if (i != inord.size() - 1) printf("%d ", inord[i]);
else printf("%d\n", inord[i]);
}
return 0;
}