关注
楼主,我也是这么想的,怎么还是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; }
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 我的求职总结 #
37561次浏览 589人参与
# 你收到了团子的OC了吗 #
1318568次浏览 11652人参与
# 一人一个landing小技巧 #
41141次浏览 722人参与
# 你觉得专业和学校哪个对薪资影响最大 #
55848次浏览 460人参与
# 国企vs私企,怎么选? #
21549次浏览 172人参与
# 机械人值得去的国央企 #
60428次浏览 408人参与
# 应届生第一份工作最好去大厂吗? #
17462次浏览 434人参与
# 考公还是考研,你怎么选? #
24965次浏览 128人参与
# 选完offer后,你后悔学本专业吗 #
43676次浏览 227人参与
# 安利/避雷我的专业 #
72193次浏览 508人参与
# 大厂还是考编 #
86976次浏览 1313人参与
# 如果重来一次你还会读研吗 #
169665次浏览 1765人参与
# 怎么防止在试用期被辞退 #
119042次浏览 896人参与
# 辞职之后最想做的一件事 #
13051次浏览 176人参与
# 联想工作体验 #
24425次浏览 166人参与
# 薪资一样,你会选择去大厂还是小公司 #
17753次浏览 106人参与
# 工作中的卑微时刻 #
9709次浏览 58人参与
# 校招第一份工作你干了多久? #
68970次浏览 338人参与
# 工作丧失热情的瞬间 #
277970次浏览 2337人参与
# 选offer应该考虑哪些因素 #
25855次浏览 345人参与