Easy-题目24:110. Balanced Binary Tree

原创 2016年05月30日 20:09:18

题目原文:
Given a binary tree, determine if it is height-balanced.
题目大意:
判断一个树是不是二叉平衡树。
题目分析:
二叉平衡树可递归定义如下:
(1) 空树是平衡二叉树。
(2) 平衡二叉树的左子树和 右子树高度差不超过1,且左右子树都是平衡二叉树。
严格按此定义设计算法即可。
源码:(language:java)

public class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root==null)
            return true;
        else if(Math.abs(getHeight(root.left)-getHeight(root.right))<=1 && isBalanced(root.left) && isBalanced(root.right))
            return true;
        else
            return false;
    }
    private int getHeight(TreeNode root)
    {
        if(root==null)
            return 0;
        else
            return Math.max(getHeight(root.left),getHeight(root.right))+1;
    }
}

成绩:
2ms,beats 23.38%,众数2ms,64.97%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

110. Balanced Binary Tree [easy] (Python)

题目链接 https://leetcode.com/problems/balanced-binary-tree/ 题目原文 Given a binary tree, determine...

110.Balanced Binary Tree从8ms到2ms到1ms

leetcode第110题,Balanced Binary Tree,判断给出的一棵二叉树是否是平衡二叉树,不断优化从用时8ms到2ms到1ms的过程,希望可以帮到和我一样的菜鸟。

剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=...

110. Balanced Binary Tree

110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For thi...

110. Balanced Binary Tree

这道题就是考查平衡二叉树的性质 描述: Given a binary tree, determine if it is height-balanced.For this problem, a he...

[leetcode]: 110. Balanced Binary Tree

1.题目Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced bina...

leetcode(110). Balanced Binary Tree

problem Given a binary tree, determine if it is height-balanced. For this problem, a height-b...

110. Balanced Binary Tree 判定是否平衡

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanc...

LeetCode110.Balanced Binary Tree题解

1. 题目描述Given a binary tree, determine if it is height-balanced. For this problem, a height-balanc...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)