-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
104 lines (83 loc) · 1.6 KB
/
main.cpp
File metadata and controls
104 lines (83 loc) · 1.6 KB
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
95
96
97
98
99
100
101
102
//
// Created by jelly on 2020/01/06.
//
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
vector<int> v;
int a[27][2];
bool check[27];
void goPreorder(int i) {
if (!check[i]) {
v.push_back(i);
check[i] = true;
}
if (a[i][0] && !check[a[i][0]]) {
goPreorder(a[i][0]);
}
if (a[i][1] && !check[a[i][1]]) {
goPreorder(a[i][1]);
}
}
void goMid(int i) {
if (!check[i]) {
check[i] = true;
}
if (a[i][0] && !check[a[i][0]]) {
goMid(a[i][0]);
}
v.push_back(i);
if (a[i][1] && !check[a[i][1]]) {
goMid(a[i][1]);
}
}
void goLast(int i) {
if (!check[i]) {
check[i] = true;
}
if (a[i][0] && !check[a[i][0]]) {
goLast(a[i][0]);
}
if (a[i][1] && !check[a[i][1]]) {
goLast(a[i][1]);
}
v.push_back(i);
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
char root, left, right;
cin >> root >> left >> right;
root -= 64;
left -= 64;
right -= 64;
if (left > 0) {
a[root][0] = left;
}
if (right > 0) {
a[root][1] = right;
}
}
goPreorder(1);
for (int c : v) {
cout << char(c + 64);
}
cout << endl;
memset(check, false, sizeof(check));
v.clear();
goMid(1);
for (int c : v) {
cout << char(c + 64);
}
cout << endl;
memset(check, false, sizeof(check));
v.clear();
goLast(1);
for (int c : v) {
cout << char(c + 64);
}
cout << endl;
return 0;
}