二叉树结点间的最大距离问题
- 套路的写法
- 改进的写法
- 完整测试代码
题目:
套路的写法:
解析:
这个也是一个二叉树套路化的问题,分为三步:
- 列出所有可能性;
- 列出结点需要的信息,并整合信息(成一个结构体);
- 改递归 ,先假设左和右都给我信息(黑盒),然后怎么利用左边和右边的信息组出来我该返回的信息,最后basecase(边界)填什么;
具体到这个题目:
第一步,列出可能性: 一个以node为头的树上,最大距离只可能来自下面三种情况:
- 不需要经过node这个点,node的左子树上自己的最大距离;
- 不需要经过node这个点,node的右子树上自己的最大距离;
- 要经过node这个点,此时就是左子树的高度+ 右子树的高度 + 1 ;
第二步,确定结点需要的信息,并整合:
- 信息一: 返回的以node为头的树的最大距离;
- 信息二: 返回的以node为头的高度;
第三步,封装信息,写出递归:
private static class Node {
public int value;
public Node left;
public Node right;
public Node (int value){
this.value = value;
}
}
private static class Pair{
public int max;
public int h;
public Pair(int max, int h) {
this.max = max;
this.h = h;
}
}
public static int maxDistance(Node head){
return process(head).max;
}
private static Pair process(Node head){
if(head == null) {
return new Pair(0,0);
}
Pair L = process(head.left);
Pair R = process(head.right);
return new Pair(Math.max((L.h + R.h + 1),Math.max(L.max,R.max)),Math.max(L.h,R.h)+1);
}
改进的写法
可以使用一个全局变量记录高度,然后max正常返回: 注意在java中要使用数组,是引用,这样的话就可以一直传递,不能使用一个变量
public static int maxDistance2(Node head){
int[] rec = new int[1];
return pos(head,rec);
}
private static int pos(Node head,int[] rec){
if(head == null){
rec[0] = 0;
return 0;
}
int L = pos(head.left,rec);
int lH = rec[0];
int R = pos(head.right,rec);
int rH = rec[0];
rec[0] = Math.max(lH,rH) + 1;
return Math.max(lH+rH+1,Math.max(L,R));
}
完整测试代码
package Algorithm.Zuo.chapter3;
public class MaxDistance {
private static class Node {
public int value;
public Node left;
public Node right;
public Node (int value){
this.value = value;
}
}
private static class Pair{
public int max;
public int h;
public Pair(int max, int h) {
this.max = max;
this.h = h;
}
}
public static int maxDistance(Node head){
return process(head).max;
}
private static Pair process(Node head){
if(head == null) {
return new Pair(0,0);
}
Pair L = process(head.left);
Pair R = process(head.right);
return new Pair(Math.max((L.h + R.h + 1),Math.max(L.max,R.max)),Math.max(L.h,R.h)+1);
}
public static int maxDistance2(Node head){
int[] rec = new int[1];
return pos(head,rec);
}
private static int pos(Node head,int[] rec){
if(head == null){
rec[0] = 0;
return 0;
}
int L = pos(head.left,rec);
int lH = rec[0];
int R = pos(head.right,rec);
int rH = rec[0];
rec[0] = Math.max(lH,rH) + 1;
return Math.max(lH+rH+1,Math.max(L,R));
}
private static Node build(int[] arr,int index){
if(index >= arr.length || arr[index] == -1)return null;
Node root = new Node(arr[index]);
root.left = build(arr,index * 2 + 1);
root.right = build(arr,index * 2 + 2);
return root;
}
public static void printTree(Node head,int height,String to,int len){
if(head == null)return;
printTree(head.right,height + 1,"v",len);
String val = to + head.value + to; //两边指示的字符
int lenV = val.length();
int lenL = (len - lenV)/2; //左边的空格(分一半)
int lenR = len - lenV - lenL; // 右边的空格
System.out.println( getSpace(len * height) + getSpace(lenL) + val + getSpace(lenR));
printTree(head.left,height + 1,"^",len);
}
//获取指定的空格
public static String getSpace(int len){
StringBuffer str = new StringBuffer();
for(int i = 0; i < len; i++) str.append(" ");
return str.toString();
}
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7};
Node head = build(arr,0);
printTree(head,0,"H",10);
System.out.println(maxDistance(head));
System.out.println(maxDistance2(head));
}
}
运行结果:(打印二叉树见这个博客)