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; && ...
随机推荐
- ubuntu18.04 安装UHD+GNU Radio
参考链接: ubuntu16.04下安装uhd与gnuradio:https://blog.csdn.net/qq_37748396/article/details/80339366 GNU Radi ...
- Visual Detail Augmented Mapping for Small Aerial Target Detection(航片动态小目标检测)
1.介绍 航片里小目标占总像元数不足1%,普通目标检测算法如YOLO会有很多错误,主要原因有3点: 1.航片的无关背景占多数 2.目标大小由于飞行高度和拍摄角度不同 3.航片中的小移动目标和噪音会混淆 ...
- 描述yeild作用
保存当前运行状态(断点),然后暂停执行,即将函数挂起 将yeild关键字后面表达式的值作为返回值返回,此时可以理解为起到了return的作用,当使用next().send()函数让函数从断点处继续执行 ...
- 「ZJOI2019」线段树
传送门 Description 线段树的核心是懒标记,下面是一个带懒标记的线段树的伪代码,其中 tag 数组为懒标记: 其中函数\(Lson(Node)\)表示\(Node\)的左儿子,\(Rson( ...
- SpringData like关键字不起作用
使用springdata简单查询时,like关键字不起作用 Hibernate: select article0_.oId as oId1_2_, article0_.articleAbstract ...
- HTTP请求响应的过程
1. TCP/IP协议分层结构 应用层(含括了OSI七层中的上三层,分别为应用层,表示层, 会话层):DNS, URI, HTML, HTTP, TLS/SSL, SMTP, POP, ...
- RabbitMQ入门学习系列(五) Exchange的Direct类型
快速阅读 利用Exchange的Direct类型,实现对队列的过滤,消费者启动以后,输入相应的key值,攻取该key值对应的在队列中的消息 . 从一节知道Exchange有四种类型 Direct,To ...
- Pycharm中直接安装第三方库
1.点File->Settings 2.在project interpreter(项目解释器)中点击“+”号 3.输入第三方库进行查询,并安排 4.安装成功
- css中的浮动与定位
传送门:https://www.cnblogs.com/junwuyao/p/7435257.html
- exp/imp 注释乱码问题或Oracle EXP-00091的解决方法
今天用imp 导入后,发现中注释乱码,源端.目的端数据库版本都是11.2.0.1 查看源端字符集: SQL> select userenv('language') from dual;USERE ...