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 时,这个二叉树视为平衡二叉树。

解题思路应该算一种分治思想。根据定义,递归地判断,实现判断。

     const int unbalanceKids = -;

     /**
* count the number of children of node and itself
*/
int height(TreeNode* node){ int heightL = ;
if (node->left != NULL){
heightL = height(node->left);
} if(heightL == unbalanceKids){
return unbalanceKids;
} int heightR = ;
if (node->right != NULL){
heightR = height(node->right);
} if(heightR == unbalanceKids){
return unbalanceKids;
} if (abs(heightL - heightR) > ){
return unbalanceKids;
} return max(heightL, heightR) + ;
} bool isBalanced(TreeNode* root) { if (root == NULL){
return true;
} int res = height(root);
if (res == unbalanceKids){
return false;
} return true;
}

[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 [Leetcode 110]Balanced Binary Tree

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

  4. Java for LeetCode 110 Balanced Binary Tree

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

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

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

  6. Leetcode 110 Balanced Binary Tree 二叉树

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

  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】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  9. Leetcode 110. Balanced Binary Tree

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

随机推荐

  1. Robotium -- 针对apk包的测试

    在使用Robotium测试的时候,有时候,测试人员并没有代码权限,而Robotium也可以在只有apk文件进行测试,下面就介绍一下这个过程. 1.设置环境变量 安装jdk环境和sdk环境 2.安装签名 ...

  2. [Angular 2] Using Two Reducers Together

    Add another reducer: export const SECOND = "SECOND"; export const HOUR = "HOUR"; ...

  3. EEPlat 的 后台业务处理模型

    后台处理包括数据处理.业务逻辑及业务流程等服务端操作的部分.相关的元模型包括业务对象元模型.业务对象属性元模型.服务元模型.參数元模型.业务规则元模型.工作流元模型.例如以下图所看到的:   业务对象 ...

  4. SQL报错error:索引中丢失IN或OUT參数

    简单记录下: 今天mybatis中遇到一个错误: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallba ...

  5. HDU4512:吉哥系列故事——完美队形I(LICS)

    Problem Description 吉哥这几天对队形比较感兴趣. 有一天,有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一 ...

  6. no drawer view found with gravity RIGHT(Android实现侧滑菜单从右面滑出) 解决办法

    代码如下: <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width ...

  7. 执行start-dfs.sh后,datenode没有启动

    Hadoop2.2.0启动异常 – Incompatible clusterIDs 2014年08月29日 ⁄ 综合 ⁄ 共 2399字 ⁄ 字号 小 中 大 ⁄ 评论关闭 今天启动Hadoop2.2 ...

  8. jQuery mini ui 2

    1.<a class="mini-button" iconCls="icon-add" onclick="addRow()" plai ...

  9. Winamp传统外观皮肤MusicRio发放

    这款皮肤是我在2002年自己弄的,如果能给还在使用Winamp的朋友使用那就最好了. 下载地址:http://files.cnblogs.com/lzhdim/MusicRio.rar

  10. RBAC角色权限控制

    RBAC角色权限控制 1. user (用户表) *  用户的基本信息(mid:用户信息id  如图) 2. node (节点表) * 页面(模块\控制器\方法) 3. role_node(角色.节点 ...