[LeetCode] 110. Balanced Binary Tree 解题思路
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 解题思路的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- Java [Leetcode 110]Balanced Binary Tree
题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- Java for LeetCode 110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
LAG()和LEAD()统计函数能够在一次查询中取出同一字段的前N行的数据和后N行的值.这样的操作能够使用对同样表的表连接来实现,只是使用LAG和 LEAD有更高的效率.下面整理的LAG()和LEAD ...
- java/php/c#版rsa签名以及java验签实现--转
在开放平台领域,需要给isv提供sdk,签名是Sdk中需要提供的功能之一.由于isv使用的开发语言不是单一的,因此sdk需要提供多种语言的版本.譬如java.php.c#.另外,在电子商务尤其是支付领 ...
- LOAD DATA INFILE Syntax--官方
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_n ...
- udp协议基础(转自疯狂java讲义)
第17章 网络编程 17.4 基于UDP协议的网络编程 UDP协议是一种不可靠的网络协议,它在通信实例的两端各建立一个Socket,但这两个Socket之间并没有虚拟链路,这两个Socket只是发 ...
- yii console.php 报错 Property "CConsoleApplication.theme" is not defined.
默认配置的话,是不会出现这个错误的,应该是有人为修改了 yiic.php 这个文件,本来是 $config 载入的应该是 console.php ,人为修改后载入了 main.php 这个配置文件了 ...
- Unix系统解压tar包时出现@LongLink错误
Unix系统上使用tar命令解压tar包后,多了一个@LongLink的文件,并且原来的tar包解压后不完整.网上查了下,原因是AIX系统上tar命令自身的一个缺陷.解决办法:把该tar包上传到lin ...
- sessionstorage,localstorage和cookie之间的区别
sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可以方便的在web请求之间保存数据.有了本地数据,就可以避免数据在浏览器和服务器间不必 ...
- mockito学习
mockito学习 写一个测试用例,如果在测试类上面添加了注解@RunWith(SpringJUnit4ClassRunner.class),必须添加@ContextConfiguration(&qu ...
- spring-data-redis问题总结
如何判断一个字符串是否是合法的long类型? 参考文档如下: http://www.oschina.net/question/59889_45179 spring-data-redis http:// ...
- react-native迁移版本遇到的问题
问题: 1. failed to find Build Tools revision 23.0.1 两个版本号需要对应