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. tomcat7源代码Bootstrap

    tomcat的启动从bootstrap的main方法開始,在main方法中主要是做了三件事,调用init方法初始化自己.调用catalinaDaemon对象 的setAwait方法设置它的await属 ...

  2. 【剑指offer】链表倒数第k个节点

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/25662121 在Cracking the Code Interview上做过了一次,这次在 ...

  3. jquery中prop()方法和attr()方法的区别(转)

    jquery1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值. 官方例举的例子感觉和attr()差不多,也不知道有什么区别,既然有了pro ...

  4. CString与std::string unicode下相互转化

      1. CString to string CString str = L"test"; CString stra(str.GetBuffer(0)); str.ReleaseB ...

  5. Android 百度地图 SDK v3.0.0 (四) 引入离线地图功能

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37758097 一直觉得地图应用支持离线地图很重要啊,我等移动2G屌丝,流量不易, ...

  6. 二叉排序树BST代码(JAVA)

        publicclassTest{     publicstaticvoid main(String[] args){         int[] r =newint[]{5,1,3,4,6,7 ...

  7. Linux下multipath多路径配置

    一.什么是多路径 普通的电脑主机都是一个硬盘挂接到一个总线上,这里是一对一的关系.而到了有光纤组成的SAN环境,或者由iSCSI组成的IPSAN环境,由于主机和存 储通过了光纤交换机或者多块网卡及IP ...

  8. 简单题思维转化BestCoder

    题意:给你a, b, c, d四个数,这几个数的范围都是大于0小于1000的整数,让比较 a ^b 和 c ^ d的大小. 这道题看着特别简单,但是当时就是做不出来,将近一个月没有做题了,手生了,不过 ...

  9. 一位学长的ACM总结(感触颇深)

    发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...

  10. mouseover,mouseout和mouseenter,mouseleave

    mouseover和mouseout 鼠标指针进入或者离开被选元素或其子元素,都会触发相应事件. 非IE浏览器支持该事件. mouseenter和mouseleave 只有在鼠标指针进入或者离开被选元 ...