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; && ...
随机推荐
- 2.json解析
static String jsonStr = "{\"sites\":[{\"name\":\"gold\",\"ur ...
- ICEM-模型导入失败的解决方法
原视频下载地址:https://yunpan.cn/cxITx5uXY6dAp 访问密码 ade8
- React.createElement 与 JSX
DOM 向JSX的演进 网页由 DOM 元素构成.React DOM 并不是浏览器的 DOM,而React DOM 只是用来告诉浏览器如何创建 DOM 的方法.通常情况下,我们并不需要 React 就 ...
- Flink(一) —— 启动与基本使用
一.Flink概念 lambda架构 将离线计算和实时计算结合在一起,发挥离线计算准确率高.实时计算响应速度快的优势.缺点是,同一套逻辑要分别在两个模式下实现,存在代码冗余的问题. Flink特点 ( ...
- 在Git中如何撤销上一次的commit
有的时候我们一不小心就git commit -m ‘commit message info’解决办法,很简单,只需执行git reset HEAD~这条命令即可,即能保证你原本的修改还在,也能撤销本次 ...
- #C++初学记录(算法测试2019/5/5)(深度搜索)
深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...
- T-MAX组--项目冲刺(第四天)
THE FOURTH DAY 项目相关 作业相关 具体描述 所属班级 2019秋福大软件工程实践Z班 作业要求 团队作业第五次-项目冲刺 作业正文 T-MAX组--项目冲刺(第四天) 团队名称 T-M ...
- 在 delphi (Object Pascal 语言)中,使用 array 关键字进行数组定义。
如果需要定义二维数组可以采取以下定义形式: 一.静态数组定义 静态数组定义,通常用于数组元素的数目确定的情况.定义形式如下: 示例: 1 2 3 4 5 6 7 8 9 10 11 type // ...
- CMU Database Systems - Timestamp Ordering Concurrency Control
2PL是悲观锁,Pessimistic,这章讲乐观锁,Optimistic,单机的,非分布式的 Timestamp Ordering,以时间为序,这个是非常自然的想法,按每个transaction的时 ...
- ThreadPoolExecutor 定制线程池参数
在 java 开发中经常需要执行一些“规格化”的任务,此时可以使用 java 线程池.节省创建线程时间,任务来时即可执行,高效. java 包是 java.util.concurrent .创建线程池 ...