본문 바로가기
Beakjoon&프로그래머스/Java

[백준/Java] 1068번 트리

by 현장 2026. 1. 28.

-Code

import java.io.*;
import java.util.*;

public class BOJ1068 {
    static ArrayList<ArrayList<Integer>> nodes;
    static int answer = 0;
    static boolean[] visited;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int cnt = Integer.parseInt(br.readLine());
        // visited
        visited = new boolean[cnt];
        // nodes
        nodes = new ArrayList<>();
        for (int i = 0; i < cnt; i++) {
            nodes.add(new ArrayList<>());
        }
        // parentsList
        StringTokenizer st = new StringTokenizer(br.readLine());
        int[] parentsList = new int[cnt];
        for (int i = 0; i < cnt; i++) {
            parentsList[i] = Integer.parseInt(st.nextToken());
        }
        // removeNode
        int removeNode = Integer.parseInt(br.readLine());
        int root = 0;
        for (int i = 0; i < cnt; i++) {
            int parents = parentsList[i];
            // root 저장
            if (parents == -1) {
                root = i;
                continue;
            }
            // 삭제된 노드인 경우나 부모가 삭제된 노드 가리키는 경우 pass
            if (i == removeNode || parents == removeNode) {
                continue;
            }
            nodes.get(parents).add(i);
        }
        if (removeNode == root) {
            System.out.println(0);
        } else {
            DFS(root);
            System.out.println(answer);
        }
    }

    private static void DFS(int nowNode) {
        // 방문 처리
        visited[nowNode] = true;
        // 다음 노드가 없으면 리프 노드
        if (nodes.get(nowNode).isEmpty()) {
            answer++;
            return;
        }
        // 탐색
        for (int next : nodes.get(nowNode)) {
            if (!visited[next]) {
                DFS(next);
            }
        }
    }
}

루트가 제거된 경우에 0을 반환하는 것을 생각 안 한 것과 제거된 노드일 경우 현재 노드가 부모 노드일 경우만 넘어가게 하고 자식 노드의 부모노드가 삭제 노드일 경우도 넘어가게 하지 못해서 2번 틀렸었습니다.