题目描述:

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.

解题思路:

递归法解题。

代码如下:

/**
* 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;
else{
if(Math.abs(getHeight(root.left) - getHeight(root.right)) > 1)
return false;
else
return isBalanced(root.left) && isBalanced(root.right);
}
} public int getHeight(TreeNode root){
if(root == null)
return 0;
else return 1 + Math.max(getHeight(root.left), getHeight(root.right));
}
}

  

Java [Leetcode 110]Balanced Binary Tree的更多相关文章

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

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

  2. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  3. Java for LeetCode 110 Balanced Binary Tree

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

  4. leetcode 110 Balanced Binary Tree ----- java

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

  5. Java实现LeetCode 110. Balanced Binary Tree

    /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...

  6. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

  7. LeetCode 110. Balanced Binary Tree (平衡二叉树)

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

  8. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  9. Leetcode 110. Balanced Binary Tree

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

随机推荐

  1. linux使用:vi编辑器

    初学linux,目前是概念多于操作,所以记录下一些操作: 编辑某个文件():vi 文件名 编辑后保存退出::wq 编辑后不保存退出: :q! 参数:-R 只读模式 -x 文件加密(vim命令下使用) ...

  2. [设计模式] 19 观察者模式 Observer Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对观察者模式是这样说的:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.当一个 ...

  3. mysqlbinlog工具基于日志恢复详细解释

    如果每天都会生成大量的二进制日志,这些日志长时间不清理的话,将会对磁盘空间带来很大的浪费,所以定期清理日志是DBA维护mysql的一个重要工作 1)RESET MASTER在上面查看日志存放的文件夹中 ...

  4. 利用vim阅读源代码一个好用的工具

    阅读源代码时常常遇到找变量,函数定义的问题.vim为我们提供了一个好用的工具,ctags. 安装 ctags. 在 libvirt的源代码根目录运行 ctags -R . vim -t virConn ...

  5. Create a method synchronized without using synchronized keyword

    Actually, lots of ways: No need for synchronization at all if you don't have mutable state. No need ...

  6. ubuntu下安装spark1.4.0

    构建在hadoop2.6.0之上的 1.在官网下载spark-1.4.0-bin-hadoop2.6.tgz 2.解压到你想要放的文件夹里,tar zxvf spark-1.4.0-bin-hadoo ...

  7. eclipse配置mahout

    1.在elcipse上建立一个java project 项目名:mymahout 2.建立libs文件夹,在mahout 0.9的lib文件夹下找到一下java包 其中log4j.properties ...

  8. android 使用sqlite的一些注意事项

    ①在Activity里创建SQLiteOpenHelper对象时,不要在成员变量里面传入context参数,而要在onCreate里面创建这个SQLiteOpenHelper对象.因为如果在成员变量里 ...

  9. Struts2 本是非单例的,与Spring集成就默认为单例

    1.Struts2本身action类是多例,此设计的原因在于本身action担任了数据载体,如果做成单例,则会便多用户数据受到影响: 2.当Struts2 与 spring整合时,Struts2的Ac ...

  10. *[topcoder]TheMatrix

    http://community.topcoder.com/stat?c=problem_statement&pm=13035 矩阵棋盘的题,比较典型.可以定两条column夹住,然后上下扫, ...