-
카테고리
-
세부 분야
알고리즘 · 자료구조
-
해결 여부
미해결
리턴 타입을 void로 해서 풀었는데 확인부탁드립니다.
23.02.01 21:59 작성 조회수 171
0
null이 아니면 계속 뻗어나가고 lt와 rt 둘다 null이면 answer 에다가 L의 최소값을 구해나가는 방식입니다.
성능의 차이가 있을까요?..
import java.util.*;
import java.io.*;
class Node {
int data;
Node lt, rt;
public Node(int data) {
this.data = data;
lt = null;
rt = null;
}
}
public class Main {
static int answer = Integer.MAX_VALUE;
public static void dfs(int L, Node root) {
if (root.lt == null && root.rt == null) {
answer = Math.min(answer, L);
} else {
if (root.lt != null) dfs(L + 1, root.lt);
if (root.rt != null) dfs(L + 1, root.rt);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
Node root = new Node(1);
root.lt = new Node(2);
root.rt = new Node(3);
root.lt.lt = new Node(5);
root.lt.rt = new Node(6);
dfs(0, root);
System.out.println(answer);
}
}
답변을 작성해보세요.
1
답변 1