-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1991.cpp
More file actions
86 lines (79 loc) · 1.25 KB
/
1991.cpp
File metadata and controls
86 lines (79 loc) · 1.25 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
#include <iostream>
#include <vector>
using namespace std;
int n;
class Node {
public:
int left = 0;
int right = 0;
bool lchild = true;
bool rchild = true;
Node() {
}
void setNodeLeft(int l) {
left = l;
}
void setNodeRight(int r) {
right = r;
}
void setlChild(bool f) {
lchild = f;
}
void setrChild(bool f) {
rchild = f;
}
};
Node node[26];
void preorder(int idx) {
cout << (char)(idx + 'A');
if (node[idx].lchild) {
preorder(node[idx].left);
}
if (node[idx].rchild) {
preorder(node[idx].right);
}
}
void inorder(int idx) {
if (node[idx].lchild) {
inorder(node[idx].left);
}
cout << (char)(idx + 'A');
if (node[idx].rchild) {
inorder(node[idx].right);
}
}
void postorder(int idx) {
if (node[idx].lchild) {
postorder(node[idx].left);
}
if (node[idx].rchild) {
postorder(node[idx].right);
}
cout << (char)(idx + 'A');
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
char c1;
char c2;
char c3;
cin >> c1 >> c2 >> c3;
if (c2 == '.') {
node[c1 - 'A'].setlChild(0);
}
else {
node[c1 - 'A'].setNodeLeft(c2 - 'A');
}
if (c3 == '.') {
node[c1 - 'A'].setrChild(0);
}
else {
node[c1 - 'A'].setNodeRight(c3 - 'A');
}
}
preorder(0);
cout << "\n";
inorder(0);
cout << "\n";
postorder(0);
}