-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1991.cpp
65 lines (57 loc) · 1.05 KB
/
1991.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
#include <iostream>
#include <vector>
using namespace std;
int N;
vector<int> tree[26];
typedef struct Node {
char label;
Node *left,*right;
Node() {
left = NULL;
right = NULL;
}
}Node;
void preorder(Node* root) {
if (root != NULL) {
cout << root->label;
preorder(root->left);
preorder(root->right);
}
}
void inorder(Node* root) {
if (root != NULL) {
inorder(root->left);
cout << root->label;
inorder(root->right);
}
}
void postorder(Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
cout << root->label;
}
}
int main() {
Node* tree = new Node[26];
cin >> N;
for (int i = 0; i < N; i++) {
char root, left, right;
cin >> root >> left >> right;
tree[root - 'A'].label = root;
if (left != '.') {
tree[left - 'A'].label = left;
tree[root - 'A'].left = &tree[left - 'A'];
}
if (right != '.') {
tree[right - 'A'].label = right;
tree[root - 'A'].right = &tree[right - 'A'];
}
}
preorder(&tree[0]);
cout << endl;
inorder(&tree[0]);
cout << endl;
postorder(&tree[0]);
return 0;
}