110. 平衡二叉树

Posted stono

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了110. 平衡二叉树相关的知识,希望对你有一定的参考价值。

110. 平衡二叉树

https://leetcode-cn.com/problems/balanced-binary-tree/description/

package com.test;

/**
 * @author stono
 * @date 2018/9/1
 */
public class Lesson110 {
    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        t1.left = t2;
        t1.right = t3;
        TreeNode.printNode(t1);
        boolean balanced = isBalanced(t1);
        System.out.println(balanced);
    }
    public static boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return judegeLeftRight(root.left,root.right);
    }

    private static boolean judegeLeftRight(TreeNode left, TreeNode right) {
        // 判断根是否是平衡的
        int res = Math.abs(treeHigh(left) - treeHigh(right));
        if (res > 1) {
            return false;
        }
        // 判断左右两个树是否是平衡的
        return isBalanced(left) && isBalanced(right);
    }

    /**
     * 计算树的高度
     * @param left
     * @return
     */
    private static int treeHigh(TreeNode left) {
        if (left == null) {
            return 0;
        }
        return 1+Math.max(treeHigh(left.left),treeHigh(left.right));
    }
}

速度不是特别快,把树的高度缓存了,还是不快:

package com.test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author stono
 * @date 2018/9/1
 */
public class Lesson110_v2 {
    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        t1.left = t2;
        t1.right = t3;
        TreeNode.printNode(t1);
        boolean balanced = isBalanced(t1);
        System.out.println(balanced);
    }
    public static boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        return judegeLeftRight(root.left,root.right);
    }

    private static boolean judegeLeftRight(TreeNode left, TreeNode right) {
        Map<TreeNode, Integer> treeNodeHighMap = new HashMap<>(8);
        // 判断根是否是平衡的
        int res = Math.abs(treeHigh(left,treeNodeHighMap) - treeHigh(right, treeNodeHighMap));
        if (res > 1) {
            return false;
        }
        // 判断左右两个树是否是平衡的
        return isBalanced(left) && isBalanced(right);
    }

    /**
     * 计算树的高度,计算过程中将TreeNode高度缓存
     * @param left
     * @return
     */
    private static int treeHigh(TreeNode left, Map<TreeNode, Integer> treeNodeHighMap) {
        // 先来取一下
        Integer integer = treeNodeHighMap.get(left);
        if (integer != null) {
            return integer;
        }
        if (left == null) {
            treeNodeHighMap.put(left, 0);
            return 0;
        }
        treeNodeHighMap.put(left, 1+Math.max(treeHigh(left.left, treeNodeHighMap),treeHigh(left.right, treeNodeHighMap)));
        return treeNodeHighMap.get(left);
    }
}

 

以上是关于110. 平衡二叉树的主要内容,如果未能解决你的问题,请参考以下文章

⭐算法入门⭐《二叉树 - 平衡二叉树》简单01 —— LeetCode 110. 平衡二叉树

LeetCode #110 平衡二叉树

代码随想录算法训练营第17天 | ● 110.平衡二叉树 ● 257. 二叉树的所有路径 ● 404.左叶子之和

110. 平衡二叉树

leetcode-----110. 平衡二叉树

LeetCode110. 平衡二叉树