package class08;
/*
* head最大距离的可能性:和head有关和无关
* 无关:左右的最大距离
* 有关:左高+右高+1
* Info信息:树最大距离,和高度
* */
public class MaxDistance {
public static class Node{
int value;
Node left;
Node right;
public Node(int value){
this.value=value;
}
}
public static class Info{
int dis;
int height;
public Info(int dis,int height){
this.dis=dis;
this.height=height;
}
}
public Info process(Node head){
if(head==null){
new Info(0,0);
}
Info leftInfo=process(head.left);
Info rightInfo=process(head.right);
int height=Math.max(leftInfo.height, rightInfo.height)+1;
int dis=Math.max(Math.max(leftInfo.dis,rightInfo.dis)
,leftInfo.height+rightInfo.height+1);
return new Info(dis,height);
}
}
二叉树套路3
最新推荐文章于 2024-07-20 09:51:47 发布