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; && ...
随机推荐
- 1045 Favorite Color Stripe (30)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favor ...
- jenkins之SSH Publishers环境变量
我使用的是docker部署jenkins,使用172.16.1.245作为部署服务器. 1.问题 在SSH Publishers里执行的环境变量,不是ssh server主机设置的环境变量,这样会导致 ...
- VsCode插件与Node.js交互通信
首先关于VsCode插件通信,如果不明白的可以参考我的这篇博客VsCode插件开发之插件初步通信 如果需要详细例子的话,可以参考VsCode插件开发 现在又有一个新的需求是,VsCode插件可以通过j ...
- Spring boot 事务Transactional
开启事务只需要加上注解@Transactional即可 // 默认情况下数据库的事务作用范围是在JapRepository 的crud上 // save 一旦执行成功,就会进行提交 // 开启事务后遇 ...
- java中快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?
一:快速失败(fail—fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的内容进行了修改(增加.删除.修改),则会抛出Concurrent Modification Exceptio ...
- mac 修改mysql root密码
01-在系统偏好设置中停止mysql服务 或者使用指令 sudo /usr/local/mysql/support-files/mysql.server stop 02-输入指令: cd /usr/l ...
- JWT签名算法
JWT签名算法 JWT签名算法中,一般有两个选择,一个采用HS256,另外一个就是采用RS256. 签名实际上是一个加密的过程,生成一段标识(也是JWT的一部分)作为接收方验证信息是否被篡改的依据. ...
- flask 设置访问地址 和 访问端口
app.run() 四个参数 host:主机,可设置为本地或其他IP port:端口,是run()启动服务的时候指定的运行端口, debug:调试,如果需要进入调试模式,可以将这个选项设置成True ...
- android 应用签名的作用
来源:https://www.jianshu.com/p/61206c96471a 1..应用程序升级:如果你希望用户无缝升级到新的版本,那么你必须用同一个证书进行签名.这是由于只有以同一个证书签名, ...
- linux安装上传下载工具lrszs
普通用户下使用sudo获取root权限,root用户直接安装: [mall@VM_0_7_centos ~]$ sudo yum -y install lrzsz Loaded plugins: fa ...