平安科技的第一题不知道边界是什么情况。

死活都是百分之80,不知道是什么情况,有大佬可以帮看看么?
题目是给出父子关系,然后计算树的高度。感觉思路没问题。。。。。。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val) {
this.val = val;
}
}


public class Main{


public static int getTreeHigh(TreeNode root) {
if(root == null)return 0;
return Math.max(getTreeHigh(root.left), getTreeHigh(root.right))+1;
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Map<Integer, TreeNode> mapTree = new HashMap<>();
int count = 0;
int rootVal = 0;
while(input.hasNextLine()) {
String guanxi = input.nextLine();
String[] fuAndZi = guanxi.split(" ");
int fatherNum = Integer.parseInt(fuAndZi[0]);
if(count == 0)rootVal = fatherNum;//根节点
int sun = Integer.parseInt(fuAndZi[1]);
TreeNode father = null;
if(mapTree.containsKey(fatherNum)) {
father = mapTree.get(fatherNum);
}else {
father = new TreeNode(fatherNum);
mapTree.put(fatherNum, father);
}
if(father.left != null) {
if(mapTree.get(sun) != null) {
father.right = mapTree.get(sun);
}else {
father.right = new TreeNode(sun);
mapTree.put(sun, father.right);
}
}else {
if(mapTree.get(sun) != null) {
father.left = mapTree.get(sun);
}else {
father.left = new TreeNode(sun);
mapTree.put(sun, father.left);
}
}
count++;
}
System.out.println(getTreeHigh(mapTree.get(rootVal)));
}
}


#平安科技#
全部评论
我的全a了 明天早上发
点赞 回复 分享
发布于 2018-09-17 01:32
根节点不一定是第一行那个父节点,考虑了吗
点赞 回复 分享
发布于 2018-09-16 23:02
0 2 1 3 1 4 0 1这种情况
点赞 回复 分享
发布于 2018-09-16 22:38

相关推荐

CARLJOSEPH...:宝宝你戾气太大了
点赞 评论 收藏
分享
本2硕9,秋招不想努力海投了
小何和:行情再不好也不可能拒绝你,不可能只要双9的
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务