leetcode第一刷_Balanced Binary Tree
二叉平衡树好火啊。差点儿每一个公司的笔试题里都有它。考了好多次我都不会,挂笔试非常有可能就是由于它。另一个它的同伙叫二叉搜索树,貌似人气比它还要高一些。
二叉平衡树是什么样的树呢。是每一个节点的左右子树高度相差绝对值都不超过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的更多相关文章
- leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal
构造方式跟中序与后序全然一样,并且一般都习惯正着来,所以更简单. 代码是之前写的,没实用库函数,不应该. TreeNode *buildIt(vector<int> &preord ...
- leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal
这道题是为数不多的感觉在读本科的时候见过的问题. 人工构造的过程是如何呢.兴许遍历最后一个节点一定是整棵树的根节点.从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右 ...
- leetcode第一刷_Validate Binary Search Tree
有了上面的教训,这道题就简单多了,什么时候该更新pre是明白的了,倒是有个细节,二叉搜索树中是不同意有相等节点的,所以题目的要求用黑体字标明了.写的时候注意就能够了. class Solution { ...
- leetcode第一刷_Unique Binary Search Trees
这道题事实上跟二叉搜索树没有什么关系,给定n个节点,让你求有多少棵二叉树也是全然一样的做法.思想是什么呢,给定一个节点数x.求f(x),f(x)跟什么有关系呢,当然是跟他的左右子树都有关系.所以能够利 ...
- leetcode第一刷_Add Binary
二进制相加,本质上就是大整数加法,有关大整数加法我的舍友教过我一个非常好的方法,先用一个int数组保存结果,将两个数相应位置相加,所有加完后.再统一处理进位的问题.这种方法相同适用于大整数的乘法. 这 ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 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 ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- ba resme
Resume Name: Zhou Heng Gender: Male Email : jackyzhouheng@gmail.com Self Assessment: I have more tha ...
- [Contest20180415]看无可看
题意:有一个数列$f$,对$\forall i\geq2,f_i=2f_{i-1}+3f_{i-2}$,给定$f_0,f_1$,再给定一个集合$S=\{a_{1\cdots n}\}$和$k$,求$\ ...
- 3.3常用类(java学习笔记)Runtime与Process
一.Runtime 我们来看下文档中对Runtime的说明: 每一个java程序都有一个属于Runtime类的实例,它允许程序连接到程序运行环境. 当前runtime可以用getRuntime()方法 ...
- 五角数 Exercise06_01
/** * @author 冰樱梦 * 题目:五角数 * 时间:2018年下半年 * * */ public class Exercise06_01 { public static void main ...
- Java多线程——ReentrantLock源码阅读
上一章<AQS源码阅读>讲了AQS框架,这次讲讲它的应用类(注意不是子类实现,待会细讲). ReentrantLock,顾名思义重入锁,但什么是重入,这个锁到底是怎样的,我们来看看类的注解 ...
- Problem B: 调用函数,求1!+2!+3!+......+10!
#include<stdio.h> double fact(int i); int main() { int i; ; ;i<=;i++) sum=sum+fact(i); prin ...
- 【ArcGIS 10.2新特性】ArcGIS 10.2将PostgreSQL原生数据发布为要素服务
1.ArcGIS 10.2支持原生数据发布为要素服 有没有将自己已有的空间数据发布为要素服务的需求?有没有将非Esri空间数据类型的数据作为服务在Web端展示的需求? ArcGIS 10.2 ...
- Nginx实现图片防盗链(referer指令)
什么是图片盗链 每张图片在浏览器中都有对应的图片地址,在浏览器中输入这个地址是可以直接拿到图片. 图片盗链,就是盗用者在他的站上需要显示我们的图片,他没有把图片拿下来,放到他的服务器上, 而是直接 ...
- oracle解决连接池不足
select count(*) from v$process;----系统有多少连接数 select value from v$parameter where name = 'processe ...
- iOS:地图笔记
地图笔记 01. CLLocation -------------------------------------------------------- CLLocationManager 定位管理者 ...