[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 ...
随机推荐
- automatically select architectures
各位在用XCode 5.x 打开用XCode 4.x 创建的项目时候.会遇到编译器警告automatically select architectures. 1. This is because th ...
- 配置本地yum源的方法
配置本地yum源的方法 1. 挂载光驱 如果是上传iso镜像到服务器上,则直接挂载iso镜像即可: #mount -o loop -t iso9660 /xxxx.iso /挂载目录 开机自动加载#v ...
- POJ 1556 The Doors 线段判交+Dijkstra
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6734 Accepted: 2670 Descrip ...
- Python基础类型
1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 names = ['Alex',"Tenglan",'Eric ...
- 图片样式 scaleType 属性
ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType),不同值的意义区别:
- css元素居中
水平居中 若为行内元素,对其父元素用text-align:center即可: 若为块元素(无浮动),则一般有两种方法可实现对其的水平居中,一为margin:0 auto;二为通过css计算函数calc ...
- Ecstore内置表单验证?
- 关于.NET中的验证码
常用的生成验证码程序 ,图片效果如下: 源程序如下: 复制代码 代码如下:using System; using System.IO; using System.Drawing; using Syst ...
- Android开发手记(7) 按钮类控件的使用
1.点击Button改变页面背景色 通过Button改变页面背景色,首先新建相应的对象,让后绑定到Layout上的元素. final RelativeLayout layout = (Relative ...
- 转:初学者,手工注入测试方法小节 (出处:: 51Testing软件测试网--jie)
1.加入单引号 ’提交, 结果:如果出现错误提示,则该网站可能就存在注入漏洞. 2.数字型判断是否有注入; 语句:and 1=1 ;and 1=2 (经典).' and '1'=1(字符型) ...