import java.util.Scanner; /** * Created by gzd on 2016/9/23. */ public class TestXiaoMiR1 { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); Node[] nodes = new Node[n]; int count = 0; // Node node = new Node(0); if(n <= 1){ System.out.println(n); return; } for(int i = 0; i < n - 1; ++i){ int father = in.nextInt(); int son = in.nextInt(); Node sonNode = new Node(son); Node fatherNode=null; for(Node a : nodes){ if(a != null&&a.value == father){ fatherNode = a; break; } if(a == null) break; } if(fatherNode == null){ fatherNode = new Node(father); nodes[count] = fatherNode; count += 1; } if(fatherNode.left == null){ fatherNode.left = sonNode; }else fatherNode.right = sonNode; nodes[count] = sonNode; count += 1; } System.out.println(FindTreeDeep(nodes[0])); } public static int FindTreeDeep(Node head){ int deep=0; if(head != null){ int lchilddeep=FindTreeDeep(head.left); int rchilddeep=FindTreeDeep(head.right); deep=lchilddeep>=rchilddeep?lchilddeep+1:rchilddeep+1; } return deep; } public static class Node{ int value; Node left; Node right; public Node(int value) { this.value = value; } } } 不知道哪错了,求指正。
点赞 评论

相关推荐

10-26 13:03
已编辑
门头沟学院 物流经理
在okr拆解的小太阳...:实习待过,我待的部门氛围很好,基本不加班,双休有保证。
投递正浩创新EcoFlow等公司10个岗位
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务