为何第一个和第四个点RE了,用数组模拟二叉树进行DFS。
public class Main {
static int[][] tree;
public static int FindHeight(int i){
if(i==0)
return 0;
else
return Math.max(FindHeight(tree[i][0]),FindHeight(tree[i][1]))+1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
tree=new int[n+1][2];
for (int i =1; i <= n; i++) {
tree[i][0]=sc.nextInt();
tree[i][1]=sc.nextInt();
}
int ans=FindHeight(1);
System.out.println(ans);
}
}