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

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/stono/p/9571989.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞