-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path중위순회.cpp
60 lines (52 loc) · 1.15 KB
/
중위순회.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
#include <iostream>
#define MAX 100
using namespace std;
char text[MAX];
int N, length;
struct Node {
char alphabet;
int child[2];
} nodes[MAX];
void input()
{
char alphabet;
int idx, child[2] = {0, };
cin >> idx >> alphabet;
if(cin.get() != '\n') {
cin >> child[0]; // 왼쪽 자식
if(cin.get() != '\n') cin >> child[1]; // 오른쪽 자식
}
idx--;
child[0]--; child[1]--;
nodes[idx].alphabet = alphabet;
nodes[idx].child[0] = child[0];
nodes[idx].child[1] = child[1];
}
void inOrder(int idx) {
if(nodes[idx].child[0] != -1) {
inOrder(nodes[idx].child[0]);
}
text[length++] = nodes[idx].alphabet;
if(nodes[idx].child[1] != -1) {
inOrder(nodes[idx].child[1]);
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
for(int t = 1; t <= 10; t++) {
cin >> N;
for(int i = 0; i < N; i++) {
input();
}
length = 0;
inOrder(0);
cout << '#' << t << ' ';
for(int i = 0; i < length; i++) {
cout << text[i];
}
cout << '\n';
}
return 0;
}