二叉树结点间的最大距离问题

二叉树结点间的最大距离问题

  • 递归
  • 改进的写法
  • 完整测试代码
题目

在这里插入图片描述

递归

解析
这个也是一个二叉树的问题,分为三步:

  • 列出所有可能性;
  • 列出结点需要的信息,并整合信息(成一个结构体);
  • 改递归 ,先假设左和右都给我信息(黑盒),然后怎么利用左边和右边的信息组出来我该返回的信息,最后basecase(边界)填什么;

具体到这个题目:
第一步,列出可能性: 一个以node为头的树上,最大距离只可能来自下面三种情况:

  • 不需要经过node这个点,node的左子树上自己的最大距离;
  • 不需要经过node这个点,node的右子树上自己的最大距离;
  • 要经过node这个点,此时就是左子树的高度 +右子树的高度+ 1

第二步,确定结点需要的信息,并整合:

  • 信息一: 返回的以node为头的树的最大距离;
  • 信息二: 返回的以node为头的高度;

第三步,封装信息,写出递归

public class Main {

    static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int value) {
            this.value = value;
        }
    }

    static class Pair {
        public int max;
        public int h;

        public Pair(int max, int h) {
            this.max = max;
            this.h = h;
        }
    }

    static int maxDistance(Node head) {
        return rec(head).max;
    }

    static Pair rec(Node head) {
        if (head == null)
            return new Pair(0, 0);
        Pair L = rec(head.left);
        Pair R = rec(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中要使用数组,是引用,这样的话就可以一直传递,不能使用一个变量

    static int maxDistance2(Node head) {
        int[] rec = new int[1];
        return rec2(head, rec);
    }

    static int rec2(Node head, int[] rec) {
        if (head == null) {
            rec[0] = 0;
            return 0;
        }
        int L = rec2(head.left, rec);
        int lH = rec[0];
        int R = rec2(head.right, rec);
        int rH = rec[0];
        rec[0] = Math.max(lH, rH) + 1;
        return Math.max(lH + rH + 1, Math.max(L, R));
    }

完整测试代码

public class MaxDistance {

    static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int value) {
            this.value = value;
        }
    }

    static class Pair {
        public int max;
        public int h;

        public Pair(int max, int h) {
            this.max = max;
            this.h = h;
        }
    }

    static int maxDistance(Node head) {
        return rec(head).max;
    }

    static Pair rec(Node head) {
        if (head == null) {
            return new Pair(0, 0);
        }
        Pair L = rec(head.left);
        Pair R = rec(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);
    }


    static int maxDistance2(Node head) {
        int[] rec = new int[1];
        return rec2(head, rec);
    }

    static int rec2(Node head, int[] rec) {
        if (head == null) {
            rec[0] = 0;
            return 0;
        }
        int L = rec2(head.left, rec);
        int lH = rec[0];
        int R = rec2(head.right, rec);
        int rH = rec[0];
        rec[0] = Math.max(lH, rH) + 1;
        return Math.max(lH + rH + 1, Math.max(L, R));
    }

    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;
    }

    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);
    }

    //获取指定的空格
    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));
    }
}

运行结果:(打印二叉树见这个博客)
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值