Skip to content

260116 : [BOJ 22856] 트리 순회#2313

Open
dlchdaud123 wants to merge 1 commit intomainfrom
chongmyoung/2312
Open

260116 : [BOJ 22856] 트리 순회#2313
dlchdaud123 wants to merge 1 commit intomainfrom
chongmyoung/2312

Conversation

@dlchdaud123
Copy link
Copy Markdown
Contributor

🚀 이슈 번호

Resolve: {#2312}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지 DFS. 재귀 호출로 전위, 중위, 후위 순회
  • 🔹 어떤 방식으로 접근했는지 재귀

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(N)
  • 이유:

💻 구현 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    // 노드 정보를 저장할 클래스 (굳이 클래스 없이 2차원 배열로도 가능하지만 가독성을 위해 사용)
    static class Node {
        int left;
        int right;

        public Node(int left, int right) {
            this.left = left;
            this.right = right;
        }
    }

    static Node[] tree;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        // 1. 노드 개수 입력
        int N = Integer.parseInt(br.readLine());
        
        // 노드 번호는 1부터 N까지이므로 N + 1 크기로 할당
        tree = new Node[N + 1];

        // 2. 트리 정보 입력
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int root = Integer.parseInt(st.nextToken());
            int left = Integer.parseInt(st.nextToken());
            int right = Integer.parseInt(st.nextToken());

            tree[root] = new Node(left, right);
        }

        // 3. 루트(1번)부터 오른쪽 방향으로만 이동하며 거리(depth) 측정
        int rightSpineCount = 0;
        int current = 1;

        while (true) {
            int rightChild = tree[current].right;
            
            if (rightChild != -1) {
                rightSpineCount++;
                current = rightChild;
            } else {
                break; // 더 이상 오른쪽 자식이 없으면 종료
            }
        }

        // 4. 공식 적용
        // 전체 왕복 이동 횟수 = 2 * (N - 1)
        // 정답 = 전체 왕복 - 오른쪽 끝까지 내려가는 경로(되돌아오지 않음)
        int totalEdges = N - 1;
        int result = 2 * totalEdges - rightSpineCount;

        System.out.println(result);
    }
}

@dlchdaud123 dlchdaud123 linked an issue Jan 16, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260116 : 코딩테스트

1 participant