楼主,我也是这么想的,怎么还是10%,你帮我看看,谢谢 public class xiaomi2 { public static void main(String[] args){ Scanner cin=new Scanner(System.in); while(cin.hasNext()){ int n=cin.nextInt(); if(n<1||n>1000){ throw new RuntimeException("error"); } List<myTree> list=new ArrayList<myTree>(); for(int i=0;i<n;i++){ list.add(new myTree(i)); } for(int i=0;i<n-1;i++){ int parent=cin.nextInt(); int child=cin.nextInt(); if((parent==child)||(parent>n-1)||(parent<0)||(child>n-1)||(child<0)){ throw new RuntimeException("error"); } if(parent>=0&&parent<n&&child>=0&&child<n){ if(list.get(parent).left==null) { list.get(parent).left=list.get(child); }else if(list.get(parent).right==null){ list.get(parent).right=list.get(child); }else { throw new RuntimeException("error"); } } } int max=0; for(int i=0;i<n;i++){ int high=getTreeHigh(list.get(0)); if(high>max) max=high; } System.out.println(max); } } private static int getTreeHigh(myTree myTree) { if(myTree==null) return 0; int left=getTreeHigh(myTree.left); int right=getTreeHigh(myTree.right); return left>=right?(left+1):(right+1); } } class myTree{ private int val; myTree(int i){ this.val=i; } public myTree left=null; public myTree right=null; }
点赞 评论

相关推荐

05-12 16:04
已编辑
江西财经大学 Java
点赞 评论 收藏
分享
牛客网
牛客企业服务