늘 겸손하게

백준 1991 - 트리 순회 본문

코딩 문제/백준

백준 1991 - 트리 순회

besforyou999 2022. 2. 20. 16:37

문제 풀이

 

각 노드의 root 값을 인덱스로 삼아 ArrayList에 저장하고 읽고 쓰는 방식을 이용하여 풀었습니다.

 

전위 순회 ( PreOrder)는 root, left, right 순,

중위 순회 ( InOrder )는 left, root, right 순

후위 순회 (PostOrder )는 left, right, root 순으로 탐색을 하는 방법입니다. 

 

 

이를 코드로 구현하면

 

전휘 순회 ( PreOrder ) : 출력이 1, left 탐색 2, right 탐색 3

1
2
3
4
5
6
7
8
    // root - left - right
    public static void preOrder(char node) throws IOException {
        if (node == '.'return;
 
        bw.write(node);
        preOrder(tree.get(node - 'A').left);
        preOrder(tree.get(node - 'A').right);
    }
cs

 

중위 순회 ( InOrder ) : left 탐색 1, 출력 2, right 탐색 3

1
2
3
4
5
6
7
8
// left - root - right
    public static void inOrder(char node) throws IOException {
        if (node == '.'return;
 
        inOrder(tree.get(node - 'A').left);
        bw.write(node);
        inOrder(tree.get(node - 'A').right);
    }
cs

 

후위 순회 ( PostOrder ) : left 탐색 1, right 탐색 2 , 출력 3

1
2
3
4
5
6
7
8
// left - right - root
    public static void postOrder(char node) throws IOException {
        if (node == '.'return;
 
        postOrder(tree.get(node - 'A').left);
        postOrder(tree.get(node - 'A').right);
        bw.write(node);
    }
cs

 

전체 코드

 

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
import java.io.*;
import java.util.*;
 
public class Main {
    static int N;
    static ArrayList<Node> tree;
    static BufferedWriter bw;
    public static void main(String [] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
        N = Integer.parseInt(br.readLine());
        tree = new ArrayList<>(N+1);
 
        for (int i = 0 ; i <= N ; i++) {
            tree.add(new Node('A','A'));
        }
 
        char rt ='A', l , r;
        StringTokenizer st;
 
        for (int i = 0 ; i < N ; i++) {
            st = new StringTokenizer(br.readLine());
            rt = st.nextToken().charAt(0);
            l = st.nextToken().charAt(0);
            r = st.nextToken().charAt(0);
            Node node = new Node(l, r);
            tree.set(rt - 'A', node);
        }
 
        preOrder('A');
        bw.write("\n");
        bw.flush();
        inOrder('A');
        bw.write("\n");
        bw.flush();
        postOrder('A');
        bw.write("\n");
        bw.flush();
    }
 
    // root - left - right
    public static void preOrder(char node) throws IOException {
        if (node == '.'return;
 
        bw.write(node);
        preOrder(tree.get(node - 'A').left);
        preOrder(tree.get(node - 'A').right);
    }
 
    // left - root - right
    public static void inOrder(char node) throws IOException {
        if (node == '.'return;
 
        inOrder(tree.get(node - 'A').left);
        bw.write(node);
        inOrder(tree.get(node - 'A').right);
    }
 
    // left - right - root
    public static void postOrder(char node) throws IOException {
        if (node == '.'return;
 
        postOrder(tree.get(node - 'A').left);
        postOrder(tree.get(node - 'A').right);
        bw.write(node);
    }
}
 
class Node {
    public char left;
    public char right;
    public Node( char l, char r) {
        left = l;
        right = r;
    }
}
 
cs