[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 ...
随机推荐
- 《火球——UML大战需求分析》(第1章 大话UML)——1.4 如何学好UML?
说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...
- php中运用GD库实现简单验证码
昨天学习了运用php的GD库进行验证码的实现. 首先可以用phpinfo()函数看一下GD库有没有安装,我用的wampserver是自动给安装的. 主要的步骤是: 1.生成验证码图片 2.随机生成字符 ...
- [转] JS nodeType返回类型
将HTML DOM中几个容易常用的属性做下记录: nodeName.nodeValue 以及 nodeType 包含有关于节点的信息. nodeName 属性含有某个节点的名称. 元素节点的 node ...
- Myeclipse+Tomcat安装与配置
一: Myeclipse安装很简单,没什么可说的,下面说一下怎么把英文版的Myeclipse汉化的问题 1.把汉化包解压,将解压后的“language”文件夹,放入Myeclipse\common文件 ...
- mongodb的地理空间索引如何在solr中体现
"$near"是唯一一个会对查询结果进行自动排序的地理空间操作符 "$near"的返回结果是按照距离由近及远排序的.其他排序条件不会生效. 这种按照地理位置远近 ...
- MySQL数据库备份与恢复方法(转)
来源于:http://www.jb51.net/article/25686.htm 网站数据对我们对站长来说都是最宝贵的,我们平时应该养成良好的备份数据的习惯. 常有新手问我该怎么备份数据库, ...
- rtsp转发服务器设计
做一个设备实时监控.需求是这样的,一个用户有多个设备(android系统,支持摄像头),设备分布在家中或者其它地方:用户可以远程通过终端(手机.pc.ipad,etc...)管理操纵这些设备(包括实时 ...
- BIOS+MBR模式 VS UEFI+GPT模式
EFI与MBR启动的区别 大硬盘和WIN8系统,让我们从传统的BIOS+MBR模式升级到UEFI+GPT模式,现在购买的主流电脑,都是预装WIN8系统,为了更好的支持2TB硬盘 ,更快速的启动win ...
- 28 Corn表达式详解 (转自http://blog.csdn.net/claram/article/details/51785193)
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...
- CSS布局注意(纯属个人总结)
和CSS样式有关多用class,和后台数据有关多用id. 1.使用绝对定位时(偏移量如:top,left...),如果父类没有相对定位,使用绝对定位的元素是相对根元素(即<html>标签) ...