LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树
110. Balanced Binary Tree
题目描述
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树的高度差的绝对值不超过 1。
每日一算法2019/5/18Day 15LeetCode110. Balanced Binary Tree
示例 1:
```
3
/ \
9 20
/ \
15 7
```
返回 true。
示例 2:
```
1
/ \
2 2
/ \
3 3
/ \
4 4
```
返回 false。
Java 实现
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Solotion {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
return Math.abs(depth(root.left) - depth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
public int depth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(depth(root.left), depth(root.right)) + 1;
}
}
相似题目
参考资料
- https://leetcode.com/problems/balanced-binary-tree/
- https://leetcode-cn.com/problems/balanced-binary-tree/
LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15的更多相关文章
- [LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)
问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- LeetCode OJ: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 bina ...
- [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode OJ】Balanced Binary Tree
Problem Link: http://oj.leetcode.com/problems/balanced-binary-tree/ We use a recursive auxilar funct ...
- 平衡二叉树Balanced Binary Tree
[抄题]: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- LeetCode算法题-Balanced Binary Tree(Java实现)
这是悦乐书的第167次更新,第169篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第26题(顺位题号是110).给定二叉树,判断它是否是高度平衡的.对于此问题,高度平衡二 ...
- LeetCode题解之Balanced Binary Tree
1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...
随机推荐
- CF1214题解
D 因为可以用贡献2把起点终点堵掉,所以答案为0/1/2 0/2简单 1:方格可以理解为分层图,考虑每个能到达终点,起点能到达其的点,标记一下,对角线如果仅存在1则为必经之路 E \(d_i\le n ...
- 标准ACL详解
- Spring Boot AOP 简易操作日志管理
AOP (Aspect Oriented Programming) 面向切面编程. 业务有核心业务和边缘业务. 比如用户管理,菜单管理,权限管理,这些都属于核心业务. 比如日志管理,操作记录管理,这些 ...
- lisp语法分析与执行分离
lisp语法分析与执行分离 <sicp>4.1.7节读书笔记 这节中将书中之前实现的解释器做了改进,将语法分析于执行分离.这样做能提高很多效率,比如我们需定义一个过程,然后执行该过程多次. ...
- 随机森林算法OOB_SCORE最佳特征选择
RandomForest算法(有监督学习),可以根据输入数据,选择最佳特征组合,减少特征冗余:原理:由于随机决策树生成过程采用的Boostrap,所以在一棵树的生成过程并不会使用所有的样本,未使用的样 ...
- iis网站搭建http访问的文件服务器
1.首先打开Internet信息服务(IIS)管理器,选择新建网站,如果没有Internet信息服务(IIS)管理器,可以在控制面板添加,按照 控制面板\程序\程序和功能,点击 打开或关闭Window ...
- laravel修改了配置文件不生效,修改了数据库配置文件不生效
Laravel缓存配置文件,因此您可能只需要清除缓存: php artisan config:clear 转: http://www.voidcn.com/article/p-sgcusrjp-bxw ...
- linux: 右键添加打开终端
安装一个包,即可在右键里面添加一个“打开终端”的菜单. sudo apt-get install nautilus-open-terminal 注销用户重启,然后再进入就可以右键->在终端打开选 ...
- 关于[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] 的解释
关于[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)] 的解释 [StructLayout(LayoutKind.Sequentia ...
- Django中的QuerySet查询优化之prefetch_related
转载的,做个笔记,原文链接 在数据库有外键的时候,使用 select_related() 和 prefetch_related() 可以很好的减少数据库请求的次数,从而提高性能.本文通过一个简单的例子 ...