能不能舅舅java选手 60分9 99
查看原帖
能不能舅舅java选手 60分9 99
345765
fldfld楼主2022/7/24 20:45
import java.util.*;
class TreeNode{
   TreeNode left;
   TreeNode right;
   int bi;
   public TreeNode(int bi){
 this.bi = bi;
       this.left = null;
       this.right = null;
}
}
public class Main{
   public static void main(String[] args){
       Scanner scan =new Scanner(System.in);

        int n= scan.nextInt();
        TreeNode [] tree = new TreeNode[n];
       for(int i = 0;i<n;i++){
         tree[i] = new TreeNode(i);
        }
       scan.nextLine();
       for(int i =0;i<n;i++){
       String strs[] = scan.nextLine().split(" ");
           int left = Integer.valueOf(strs[0]);
           int right =Integer.valueOf(strs[1]);
           if(left> n || right > n){
               return;
           }
           if(left != 0 && right !=0){
         tree[i].left = tree[left-1];
         tree[i].right = tree[right-1];
           }else if(left == 0 && right != 0){
               tree[i].left = null;
               tree[i].right = tree[right-1];
           }else if(left != 0 && right == 0){
                tree[i].left = tree[left-1];
               tree[i].right = null;
           }else{
                tree[i].left = null;
                  tree[i].right = null;
           }
        
          
       }
       int max = 0;
       // for(int i  =0;i<n;i++){
       // int res = getDepth(tree[i]);
       //    max = Math.max(max,res);
       // }
       System.out.println(getDepth(tree[0]));
   }
   public static int getDepth(TreeNode tree){
       if(tree == null){
          
           return 0;
       }
       int a = getDepth(tree.left);
       int b = getDepth(tree.right);
      
       return Math.max(a,b)+1;
   }
}



import java.util.*;
class TreeNode{
   TreeNode left;
   TreeNode right;
   int bi;
   public TreeNode(int bi){
 this.bi = bi;
       this.left = null;
       this.right = null;
}
}
public class Main{
   public static void main(String[] args){
       Scanner scan =new Scanner(System.in);

        int n= scan.nextInt();
        TreeNode [] tree = new TreeNode[n];
       for(int i = 0;i<n;i++){
         tree[i] = new TreeNode(i);
        }
       scan.nextLine();
       for(int i =0;i<n;i++){
       String strs[] = scan.nextLine().split(" ");
           int left = Integer.valueOf(strs[0]);
           int right =Integer.valueOf(strs[1]);
           if(left> n || right > n){
               return;
           }
           if(left != 0 && right !=0){
         tree[i].left = tree[left-1];
         tree[i].right = tree[right-1];
           }else if(left == 0 && right != 0){
               tree[i].left = null;
               tree[i].right = tree[right-1];
           }else if(left != 0 && right == 0){
                tree[i].left = tree[left-1];
               tree[i].right = null;
           }else{
                tree[i].left = null;
                  tree[i].right = null;
           }
        
          
       }
       int max = 0;
       // for(int i  =0;i<n;i++){
       // int res = getDepth(tree[i]);
       //    max = Math.max(max,res);
       // }
       System.out.println(getDepth(tree[0]));
   }
   public static int getDepth(TreeNode tree){
       if(tree == null){
          
           return 0;
       }
       int a = getDepth(tree.left);
       int b = getDepth(tree.right);
      
       return Math.max(a,b)+1;
   }
}



2022/7/24 20:45
加载中...