트리 순회22856

LJM·2023년 7월 20일
0

백준풀기

목록 보기
192/259

https://www.acmicpc.net/problem/22856

파싱할때 트리구조를 만들어 주는 부분
중위순회의 마지막 멈추는부분
이동할때마다 카운트 하는 부분이 어러웠지만
풀어냈다~

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


class Node{

    Node left;
    Node right;
    Node(){

    }
}

public class Main {
    static int answer = 0;
    static int stopper = 0;

    static int n = 0;
    public static void main(String[] args) throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        n = Integer.parseInt(br.readLine());

        String[] input;

        HashMap<Integer, Node> nodes = new HashMap<>();

        for (int i = 0; i < n; i++) {
            input = br.readLine().split(" ");

            int val = Integer.parseInt(input[0]);
            int left = Integer.parseInt(input[1]);
            int right = Integer.parseInt(input[2]);

            if(left != -1 && nodes.containsKey(left) == false)
                nodes.put(left, new Node());
            if(right != -1 && nodes.containsKey(right) == false)
                nodes.put(right, new Node());

            if(nodes.containsKey(val) == false){

                Node cur = new Node();
                if(left != -1){
                    cur.left = nodes.get(left);
                }
                if(right != -1){
                    cur.right = nodes.get(right);
                }
                nodes.put(val, cur);
            }else{
                Node cur = nodes.get(val);
                if(left != -1){
                    cur.left = nodes.get(left);
                }
                if(right != -1){
                    cur.right = nodes.get(right);
                }
            }
        }

        answer = dfs(nodes.get(1), 0);

        System.out.println(answer);
    }

    public static int dfs(Node parent, int count){


        if(parent.left != null)
            count = dfs(parent.left, count+1);

        stopper++;

        if(parent.right!= null)
            count = dfs(parent.right, count+1);


        if(stopper == n){
            return count;
        }else{
            return count+1;
        }
    }
}
profile
게임개발자 백엔드개발자

1개의 댓글

comment-user-thumbnail
2023년 7월 20일

좋은 글 잘 읽었습니다, 감사합니다.

답글 달기