-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.11
49 lines (40 loc) · 979 Bytes
/
11.11
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 <unordered_map>
using namespace std;
unordered_map<char, pair<char, char>> tree;
void preorder(char node) {
if (node == '.') return;
cout << node;
preorder(tree[node].first);
preorder(tree[node].second);
}
// 중위 순회 (Inorder Traversal)
void inorder(char node) {
if (node == '.') return;
inorder(tree[node].first);
cout << node;
inorder(tree[node].second);
}
void postorder(char node) {
if (node == '.') return;
postorder(tree[node].first);
postorder(tree[node].second);
cout << node;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
char node, left, right;
cin >> node >> left >> right;
tree[node] = {left, right};
}
// 순회 결과 출력
preorder('A');
cout << endl;
inorder('A');
cout << endl;
postorder('A');
cout << endl;
return 0;
}