JAVA一、四样例RE求助!
查看原帖
JAVA一、四样例RE求助!
866144
holo_23楼主2023/10/7 19:42

为何第一个和第四个点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);
    }
    //[0,,2,3,]
}

2023/10/7 19:42
加载中...