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. SPOJ PGCD (mobius反演 + 分块)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意 :求满足gcd(i , j)是素数(1 &l ...

  2. Qt中使用cout, cin, cerr

      在Qt中,用的最多的是GUI页面,但GUI比较慢,有些小的试验不妨先用console,当然在Qt里面可以直接使用cout, cin这些. 不过可能不会兼容Qt自带的一些类型,比如QByteArra ...

  3. [转] GDB 下 watch的使用

    这里大概说下gdb调试程序时,watch的使用.至于原理尚不清楚,以后再做补充,还请见谅. watch通常需要和break,run,continue联合使用. 下面举例说明: 代码如下: #inclu ...

  4. (转)关于c#中的事件

    原文链接http://blog.csdn.net/joyhen/article/details/8500211 如有不明白的地方欢迎加QQ群14670545 探讨 最近在看委托,然后看到事件,以前一直 ...

  5. “The SQL Server license agreenment cannot be located for the selected edition.”MSSQL安装问题

    今天老邹又来吐槽了.今天不和IE7较劲了.说点别的吧. 我呢什么软件都喜欢装最新版的,这部刚出来windows 8.1就赶紧装上了,随后就用上了vs2013.前天看到新闻说微软已经发布了sql ser ...

  6. PL/SQL中的变量

    1.标量: ag1: declare v_ename emp.ename%type;--自己称为单变量 begin select ename into v_ename from emp where e ...

  7. UIWebView禁止点击后跳转

    #pragma mark 禁止webview中的链接点击 - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRe ...

  8. 武汉科技大学ACM:1008: 零起点学算法64——回型矩阵

    Problem Description 输出n*m的回型矩阵 Input 多组测试数据 每组输入2个整数 n和m(不大于20) Output 输出n*m的回型矩阵,要求左上角元素是1,(每个元素占2个 ...

  9. 武汉科技大学ACM :1010: 零起点学算法103——一只小蜜蜂...

    Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. Input 输入数据的第一 ...

  10. 3.1 as86汇编器

    在开始讲述as86汇编器前,这本书引用内核中bootsect.s框架程序汇编代码来解释,记录下这一小段代码中不理解的地方,下面是这段实例代码: .globl begtext, begdata, beg ...