二叉平衡树好火啊。差点儿每一个公司的笔试题里都有它。考了好多次我都不会,挂笔试非常有可能就是由于它。另一个它的同伙叫二叉搜索树,貌似人气比它还要高一些。

二叉平衡树是什么样的树呢。是每一个节点的左右子树高度相差绝对值都不超过1。好。你说你最终回了,这不非常easy吗,求一下根节点的左右字数高度,假设满足,他就是。否则就不是嘛。不是啊亲,要求是全部节点都满足这个条件,推断的时候必须每一个节点都验证的!

扯了这么长。事实上看看代码就明确了,怎么有种在贴吧发言要凑够15字的感觉。

int getHeight(TreeNode *root){
if(root == NULL) return 0;
if(!root->left&&!root->right) return 1;
return max(getHeight(root->left), getHeight(root->right))+1;
}
class Solution {
public:
bool isBalanced(TreeNode *root) {
if(root == NULL) return true;
if(!root->left && !root->right) return true;
if(isBalanced(root->left)&&isBalanced(root->right)&&abs(getHeight(root->left)-getHeight(root->right))<=1)
return true;
return false;
}
};

leetcode第一刷_Balanced Binary Tree的更多相关文章

  1. leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal

    构造方式跟中序与后序全然一样,并且一般都习惯正着来,所以更简单. 代码是之前写的,没实用库函数,不应该. TreeNode *buildIt(vector<int> &preord ...

  2. leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal

    这道题是为数不多的感觉在读本科的时候见过的问题. 人工构造的过程是如何呢.兴许遍历最后一个节点一定是整棵树的根节点.从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右 ...

  3. leetcode第一刷_Validate Binary Search Tree

    有了上面的教训,这道题就简单多了,什么时候该更新pre是明白的了,倒是有个细节,二叉搜索树中是不同意有相等节点的,所以题目的要求用黑体字标明了.写的时候注意就能够了. class Solution { ...

  4. leetcode第一刷_Unique Binary Search Trees

    这道题事实上跟二叉搜索树没有什么关系,给定n个节点,让你求有多少棵二叉树也是全然一样的做法.思想是什么呢,给定一个节点数x.求f(x),f(x)跟什么有关系呢,当然是跟他的左右子树都有关系.所以能够利 ...

  5. leetcode第一刷_Add Binary

    二进制相加,本质上就是大整数加法,有关大整数加法我的舍友教过我一个非常好的方法,先用一个int数组保存结果,将两个数相应位置相加,所有加完后.再统一处理进位的问题.这种方法相同适用于大整数的乘法. 这 ...

  6. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  8. LeetCode——Serialize and Deserialize Binary Tree

    Description: Serialization is the process of converting a data structure or object into a sequence o ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. POJ 1912 A highway and the seven dwarfs (凸包)

    [题目链接] http://poj.org/problem?id=1912 [题目大意] 给出一些点,表示一些屋子,这些屋子共同组成了村庄,现在要建一些高速公路 问是否经过了村庄. [题解] 这些屋子 ...

  2. Java多线程——锁概念与锁优化

    为了性能与使用的场景,Java实现锁的方式有非常多.而关于锁主要的实现包含synchronized关键字.AQS框架下的锁,其中的实现都离不开以下的策略. 悲观锁与乐观锁 乐观锁.乐观的想法,认为并发 ...

  3. ThinkPHP模板中JS等带花括号处会被解析错误的解决办法

    如下图,当本人在ThinkPHP框架的模板中写jQuery代码的时候,写了一些注释,并且注重是斜线和换括号{是连着一起的,这层语法上来时是没问题的,但是在ThinkPHP 的模板引擎解析下,会被解析掉 ...

  4. 7zip File: How to Uncompress 7z files on Ubuntu, Debian, Fedora

    转:http://www.thegeekstuff.com/2010/04/7z-7zip-7za-file-compression/ Question: How do I uncompress a ...

  5. 【mybatis】 mybatis在mysql 更新update 操作 更新时间字段按照年月日时分秒格式 更新为当前时间

    示例代码如下: update goods_msg SET create_date = DATE_FORMAT(NOW(),'%Y-%m-%d %H:%m:%s') WHERE uid = '6183b ...

  6. 【JVM】Myecplise自带的JVM大小调整,用于Junit等测试时使用

    一般在使用Junit或者一个工具类的main方法执行时,在Myecplise中运行,并不会占用多大的堆空间.如果出现OutofMemory错误,调整MyEcplise自带的JVM大小. 在Myecpl ...

  7. druid 配置监控界面和开启spring支持

    1.配置监控页面 <!-- JNDI方式配置数据源 --> <!-- <bean id="dataSource" class="org.sprin ...

  8. grub

    root (hd0,0)    kernel /vmlinuz-2.6.32-573.8.1.el6.i686 ro root=/dev/mapper/VolGroup-lv_root nomodes ...

  9. Python - 文本处理模块

    文本处理模块 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27050431 Python的文本处理模块, 使用四种内置库. st ...

  10. mysql性能检查脚本-部分

    #!/bin/sh #ocpyang@126.com export black='\033[0m' export boldblack='\033[1;0m' export red='\033[31m' ...