3.5 二叉树递归套路

1. 判断二叉树是完全二叉树

完全二叉树,若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。

    class Info5 {
        // 子树高度
        public int height;
        // 子树是否是满二叉树
        public boolean full;
        // 子树是否是完全二叉树
        public boolean cbt;

        public Info5(int height, boolean full, boolean cbt) {
            this.height = height;
            this.full = full;
            this.cbt = cbt;
        }
    }

    public Info5 processCBT(BiNode node) {
        if (node == null) {
            return new Info5(0, true, true);
        }
        Info5 left = processCBT(node.left);
        Info5 right = processCBT(node.right);
        int height = Math.max(left.height, right.height) + 1;
        boolean full = left.full && right.full && left.height == right.height;
        boolean cbt = full
                || (left.cbt && right.full && left.height - right.height == 1)
                || (left.full && right.full && left.height - right.height == 1)
                || (left.full && right.cbt && left.height == right.height);
        return new Info5(height, full, cbt);
    }

    public boolean isCompleteTreeCompare(BiNode node) {
        return processCBT(node).cbt;
    }

    // 完全二叉树
    public boolean isCompleteTree(BiNode node) {
        if (node == null) {
            return true;
        }
        Queue<BiNode> queue = new LinkedList<>();
        queue.add(node);
        BiNode l, r;
        BiNode node1;
        boolean isLeaf = false;
        while (!queue.isEmpty()) {
            node1 = queue.poll();
            l = node1.left;
            r = node1.right;
            // 一旦开始叶节点遍历,左右节点都应该为空
            if (isLeaf && (l != null || r != null)) {
                return false;
            }
            if (l == null && r != null) {
                return false;
            }
            if (l != null) {
                queue.add(l);
            }
            if (r != null) {
                queue.add(r);
            }
            isLeaf = !(l != null && r != null);
        }
        return true;
    }

    @Test
    public void test1() {
        for (int i = 0; i < 10000; i++) {
            BiNode node = Reduce.binaryTree(5, 100);
            boolean r1 = isCompleteTree(node);
            boolean r2 = isCompleteTreeCompare(node);
            if (r1 != r2) {
                System.out.println(Printer.print(node));
                System.out.println(r1);
                System.out.println(r2);
                return;
            }
        }
    }

2. 头节点是 head, 求任一两个节点 a, b 的最低公共父节点


    class Info7 {
        public boolean findA;
        public boolean findB;
        public BiNode node;

        public Info7(boolean findA, boolean findB, BiNode node) {
            this.findA = findA;
            this.findB = findB;
            this.node = node;
        }
    }

    public BiNode lowParent(BiNode head, BiNode a, BiNode b) {
        return processLowPrt(head, a, b).node;
    }

    private Info7 processLowPrt(BiNode head, BiNode a, BiNode b) {
        if (head == null) {
            return new Info7(false, false, null);
        }
        Info7 left = processLowPrt(head.left, a, b);
        Info7 right = processLowPrt(head.right, a, b);
        boolean findA = left.findA || right.findA || head == a,
                findB = left.findB || right.findB || head == b;
        BiNode node = left.node != null ? left.node : right.node != null ? right.node : findA && findB ? head : null;
        return new Info7(findA, findB, node);
    }

    public BiNode lowParentCompare(BiNode node, BiNode a, BiNode b) {
        if (node == null || a == null || b == null) {
            return null;
        }
        Map<BiNode, BiNode> node2parent = new HashMap<>();
        Stack<BiNode> stack = new Stack<>();
        stack.push(node);
        while (!stack.isEmpty()) {
            BiNode n = stack.pop();
            if (n.right != null) {
                node2parent.put(n.right, n);
                stack.push(n.right);
            }
            if (n.left != null) {
                node2parent.put(n.left, n);
                stack.push(n.left);
            }
        }
        // 生成链表,找第一个相交节点,然后就是两个链表的距离
        Set<BiNode> set = new HashSet<>();
        // 先把 p1 全放进去
        while (a != null) {
            set.add(a);
            a = node2parent.get(a);
        }
        // 找到相交节点
        while (b != null) {
            if (!set.add(b)) {
                return b;
            }
            b = node2parent.get(b);
        }
        return null;
    }

    @Test
    public void test3() {
        for (int i = 0; i < 10000; i++) {
            BiNode node = Reduce.binaryTree(10, 100);
            List<BiNode> nodes = Reduce.elements(node);
            BiNode a = null, b = null;
            if (nodes.size() > 0) {
                a = nodes.get(Reduce.num(0, nodes.size() - 1));
                b = nodes.get(Reduce.num(0, nodes.size() - 1));
            }
            BiNode r1 = lowParent(node, a, b);
            BiNode r2 = lowParentCompare(node, a, b);
            if (r1 != r2) {
                System.out.println(Printer.print(node));
                System.out.println(r1);
                System.out.println(r2);
                return;
            }
        }
    }

3. 判断是否是满二叉树


    class Info6 {
        public int height;
        public boolean full;

        public Info6(int height, boolean full) {
            this.height = height;
            this.full = full;
        }
    }

    public boolean isFull(BiNode node) {
        return processFull(node).full;
    }

    private Info6 processFull(BiNode node) {
        if (node == null) {
            return new Info6(0, true);
        }
        Info6 left = processFull(node.left);
        Info6 right = processFull(node.right);
        return new Info6(
                Math.max(left.height, right.height) + 1,
                left.full && right.full && left.height == right.height
        );
    }

    public boolean isFullCompare(BiNode node) {
        if (node == null) {
            return true;
        }
        Queue<BiNode> queue = new LinkedList<>();
        queue.add(node);
        BiNode end = node, nextEnd = null;
        int num = 0, level = 0;
        while (!queue.isEmpty()) {
            BiNode n = queue.poll();
            num++;
            if (n.left != null) {
                queue.add(n.left);
                nextEnd = n.left;
            }
            if (n.right != null) {
                queue.add(n.right);
                nextEnd = n.right;
            }
            if (n == end) {
                level++;
                end = nextEnd;
            }
        }
        return num == (((int) Math.pow(2, level)) - 1);
    }

    @Test
    public void test2() {
        for (int i = 0; i < 10000; i++) {
            BiNode node = Reduce.binaryTree(20, 100);
            boolean r1 = isFull(node);
            boolean r2 = isFullCompare(node);
            if (r1 != r2) {
                System.out.println(Printer.print(node));
                System.out.println(r1);
                System.out.println(r2);
                return;
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值