How We Coding

[1991] 트리 순회

BOJ/Tree2018. 5. 20. 01:19

[1991] 트리 순회 : http://boj.kr/1991


<소스코드>


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
 
#include <stdio.h>
 
char tree[26][2];
 
void pre(int root)
{
    if(root == '.'return ;
    printf("%c", root+'A');
    pre(tree[root][0]);
    pre(tree[root][1]);
}
 
void in(int root)
{
    if(root == '.'return ;
    in(tree[root][0]);
    printf("%c", root+'A');
    in(tree[root][1]);
}
 
void post(int root)
{
    if(root == '.'return ;
    post(tree[root][0]);
    post(tree[root][1]);
    printf("%c", root+'A');
}
 
int main()
{
    int n;
    scanf("%d"&n);
 
    while(n--) {
        char p, lc, rc;
        scanf(" %c %c %c"&p, &lc, &rc);
        tree[p-'A'][0= lc == '.' ? lc : lc-'A';
        tree[p-'A'][1= rc == '.' ? rc : rc-'A';
    }
 
    pre(0); puts("");
    in(0); puts("");
    post(0); puts("");
    
    return 0;
}
cs

'BOJ > Tree' 카테고리의 다른 글

[4803] 트리  (0) 2018.05.21
[11725] 트리의 부모 찾기  (0) 2018.05.20