Skip to content

Commit 67a7d5a

Browse files
committed
[BOJ] 1991 트리 순회 (S1)
1 parent 0370be4 commit 67a7d5a

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

박예진/0주차/트리/1991.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//https://www.acmicpc.net/problem/1991
2+
#include <iostream>
3+
#include <algorithm>
4+
#include <vector>
5+
#include <cstring>
6+
7+
using namespace std;
8+
9+
struct Node {
10+
char left;
11+
char right;
12+
} node[26];
13+
14+
// 전위순회
15+
void preOrder(char alpha){
16+
if (alpha == '.') return;
17+
char left = node[alpha - 'A'].left;
18+
char right = node[alpha - 'A'].right;
19+
20+
cout << alpha;
21+
preOrder(left);
22+
preOrder(right);
23+
}
24+
25+
void inOrder(char alpha) {
26+
if (alpha == '.') return;
27+
char left = node[alpha - 'A'].left;
28+
char right = node[alpha - 'A'].right;
29+
30+
inOrder(left);
31+
cout << alpha;
32+
inOrder(right);
33+
}
34+
35+
void postOrder(char alpha) {
36+
if (alpha == '.') return;
37+
char left = node[alpha - 'A'].left;
38+
char right = node[alpha - 'A'].right;
39+
40+
postOrder(left);
41+
postOrder(right);
42+
cout << alpha;
43+
}
44+
45+
int main(){
46+
ios_base::sync_with_stdio(false);
47+
cin.tie(NULL); cout.tie(NULL);
48+
49+
int N;
50+
cin >> N;
51+
for(int i = 0; i < N; i++){
52+
char X, L, R;
53+
cin >> X >> L >> R; // 노드, 왼쪽, 오른쪽
54+
node[X - 'A'].left = L;
55+
node[X - 'A'].right = R;
56+
}
57+
58+
preOrder('A');
59+
cout << "\n";
60+
61+
inOrder('A');
62+
cout << "\n";
63+
64+
postOrder('A');
65+
}

0 commit comments

Comments
 (0)