public static void main(String[] args){
HashMap<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int max = 0;
for (int i = 1; i <= N; i++) {
int left = scan.nextInt();
int right = scan.nextInt();
if(left==0 && right==0)
continue;
Integer faH = map.get(i);
map.put(left, faH + 1);
map.put(right, faH + 1);
max = Math.max(max, faH + 1);
}
System.out.println(max);
}
我的思路是 不建树 只统计每个节点和相应高度,输出最大值