문제 풀이 및 개발 공간

[백준] 5639번 이진 검색 트리 (gold 5 본문

백준공부/java

[백준] 5639번 이진 검색 트리 (gold 5

gomduri43 2023. 10. 15. 14:48

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

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        Node root=new Node(Integer.valueOf(br.readLine()), null, null);

        String input;
        while((input=br.readLine())!=null && input.length()!=0){
            in(root,Integer.valueOf(input));
        }
        find(root);

    }
    public static void in(Node temp, int a){
        if(temp.data<a){
            if(temp.right==null){
                temp.right=new Node(a,null,null);
            }
            else{
                in(temp.right,a);
            }
        }
        else{
            if(temp.left==null){
                temp.left=new Node(a,null,null);
            }
            else{
                in(temp.left,a);
            }
        }

    }
    public static void find(Node temp){
        if(temp.left!=null){
            find(temp.left);
        }
        if(temp.right!=null){
            find(temp.right);
        }
       System.out.println(temp.data);
        return;

    }
}