-
Notifications
You must be signed in to change notification settings - Fork 2
/
1991_josueyeon.cpp
109 lines (84 loc) · 2.23 KB
/
1991_josueyeon.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 1991 트리 순회
// - pre-order: root->left->right
// - in-order: left->root->right
// - post-order: left->right->root
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void preorder(vector<pair<char, char>> graph[], bool visited[], int node)
{
if (visited[node] == true || node == -1)
return;
char ch = node + 65;
cout<<ch;
visited[node] = true;
for(int i = 0;i < graph[node].size();i++)
{
int left = graph[node][i].first;
int right = graph[node][i].second;
preorder(graph, visited, left);
preorder(graph, visited, right);
}
}
void inorder(vector<pair<char, char>> graph[], bool visited[], int node)
{
if (visited[node] == true || node == -1)
return;
visited[node] = true;
for(int i = 0;i < graph[node].size();i++)
{
int left = graph[node][i].first;
int right = graph[node][i].second;
inorder(graph, visited, left);
char ch = node + 65;
cout<<ch;
inorder(graph, visited, right);
}
}
void postorder(vector<pair<char, char>> graph[], bool visited[], int node)
{
if (visited[node] == true || node == -1)
return;
visited[node] = true;
for(int i = 0;i < graph[node].size();i++)
{
int left = graph[node][i].first;
int right = graph[node][i].second;
postorder(graph, visited, left);
postorder(graph, visited, right);
char ch = node + 65;
cout<<ch;
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
// N(node)
int N;
cin>>N;
// A:1 ~ Z: 26
vector<pair<char, char>> graph[26];
bool visited[26];
for(int i = 0;i < N;i++)
{
char A, B, C;
cin>>A>>B>>C;
B = (B == '.') ? -1 : B - 65;
C = (C == '.') ? -1 : C - 65;
graph[A - 65].push_back({B, C});
}
// preorder
fill_n(visited, 26, false);
preorder(graph, visited, 0);
cout<<"\n";
// inorder
fill_n(visited, 26, false);
inorder(graph, visited, 0);
cout<<"\n";
// postorder
fill_n(visited, 26, false);
postorder(graph, visited, 0);
cout<<"\n";
}