您的位置:首页 > 其它

现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度

2017-09-18 15:25 405 查看
现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度

输入描述:

输入的第一行表示节点的个数n(1 ≤ n ≤ 1000,节点的编号为0到n-1)组成,

下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号

输出描述:

输出树的高度,为一个整数

示例1

输入

5

0 1

0 2

1 3

1 4

输出

3

一段时间没写java了,竟然连建树都不会了。。。

这个题并不难,大体思路是:对给定的一棵树root,Height=max(root.left,root.right)+1。

感觉困难的就是输入困难一点

import java.util.Scanner;
import java.util.HashMap;
public class Main {
public static class TreeNode{
TreeNode left=null;
TreeNode right=null;
int value;
public TreeNode(int v){
this.value=v;
}
}
public int Height(TreeNode root){
if(root==null){
return 0;
}
int left=0,right=0;
if(root.left!=null){
left=Height(root.left);
}
if(root.right!=null){
right=Height(root.right);
}
return ((left>=right)?left:right)+1;
}
public static void main(String[] args) {
Main m=new Main();
Scanner s=new Scanner(System.in);
int num=s.nextInt();
int rootval=s.nextInt();
int rootfirst=s.nextInt();

TreeNode root=new TreeNode(rootval);
TreeNode rootLeft=new TreeNode(rootfirst);
root.left=rootLeft;

HashMap<Integer,TreeNode> nodeMap=new HashMap<Integer,TreeNode>();
nodeMap.put(rootval, root);
nodeMap.put(rootfirst,rootLeft);

for(int i=0;i<num-2;i++){
int parentval=s.nextInt();
int childval=s.nextInt();
TreeNode parent=nodeMap.get(parentval);
TreeNode child=new TreeNode(childval);
if(parent!=null){
if(parent.left==null){
parent.left=child;
}else if(parent.right==null){
parent.right=child;
}
}else{
parent=new TreeNode(parentval);
parent.left=child;
}
nodeMap.put(parentval, parent);
nodeMap.put(childval, child);
}
System.out.println(m.Height(root));
s.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐