[LeetCode]--110. Balanced Binary Tree

来源:互联网 发布:淘宝卖家10元补拍图片 编辑:程序博客网 时间:2024/06/06 03:31

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

利用求最大深度,然后再求两边相减的绝对值如果是小于1的就行,然后如果所有子树都是平衡二叉树那么大树那必然是平衡二叉树。

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public boolean isBalanced(TreeNode root) {    if (root == null)            return true;        if (root.left == null && root.right == null)            return true;        if (root.left == null && root.right != null && root.right.left == null                && root.right.right == null)            return true;        if (root.left != null && root.right == null && root.left.left == null                && root.left.right == null)            return true;        if (root.left != null && root.right != null                && Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1)            return isBalanced(root.left) && isBalanced(root.right);        else            return false;    }    public int maxDepth(TreeNode root) {        if (root == null)            return 0;        if (root.left == null && root.right == null)            return 1;        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;    }}

别人写的代码第一个版本。

// Version 1: with ResultType/** * Definition of TreeNode: * public class TreeNode { *     public int val; *     public TreeNode left, right; *     public TreeNode(int val) { *         this.val = val; *         this.left = this.right = null; *     } * } */class ResultType {    public boolean isBalanced;    public int maxDepth;    public ResultType(boolean isBalanced, int maxDepth) {        this.isBalanced = isBalanced;        this.maxDepth = maxDepth;    }}public class Solution {    /**     * @param root: The root of binary tree.     * @return: True if this Binary tree is Balanced, or false.     */    public boolean isBalanced(TreeNode root) {        return helper(root).isBalanced;    }    private ResultType helper(TreeNode root) {        if (root == null) {            return new ResultType(true, 0);        }        ResultType left = helper(root.left);        ResultType right = helper(root.right);        // subtree not balance        if (!left.isBalanced || !right.isBalanced) {            return new ResultType(false, -1);        }        // root not balance        if (Math.abs(left.maxDepth - right.maxDepth) > 1) {            return new ResultType(false, -1);        }        return new ResultType(true, Math.max(left.maxDepth, right.maxDepth) + 1);    }}

我觉得第一个版本太啰嗦了,看第二个版本吧。这个真的是值得学习,这个if (left == -1 || right == -1 || Math.abs(left-right) > 1)判断语句值得深思。

// Version 2: without ResultTypepublic class Solution {    public boolean isBalanced(TreeNode root) {        return maxDepth(root) != -1;    }    private int maxDepth(TreeNode root) {        if (root == null) {            return 0;        }        int left = maxDepth(root.left);        int right = maxDepth(root.right);        if (left == -1 || right == -1 || Math.abs(left-right) > 1) {            return -1;        }        return Math.max(left, right) + 1;    }}
0 0