【LeetCode】Balanced Binary Tree 解题报告
【题目】
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
【说明】
不是非常难,思路大家可能都会想到用递归,分别推断左右两棵子树是不是平衡二叉树,假设都是而且左右两颗子树的高度相差不超过1。那么这棵树就是平衡二叉树。
【比較直观的Java代码】
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
if(root.left==null && root.right==null){
return true;
}
if(Math.abs(depth(root.left)-depth(root.right)) > 1){
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
} public int depth(TreeNode root){
if(root==null){
return 0;
}
return 1+Math.max(depth(root.left), depth(root.right));
}
}
【改进后】
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
height(root);
return run(root);
} public boolean run(TreeNode root) {
if (root == null) return true; int l = 0, r = 0;
if (root.left != null) l = root.left.val;
if (root.right != null) r = root.right.val;
if (Math.abs(l - r) <= 1 && isBalanced(root.left) && isBalanced(root.right)) return true; return false;
} public int height(TreeNode root) {
if (root == null) return 0;
root.val = Math.max( height(root.left), height(root.right) ) + 1;
return root.val;
}
}
利用了TreeNode结构中的val,用它来记录以当前结点为根的子树的高度,避免多次计算。
【LeetCode】Balanced Binary Tree 解题报告的更多相关文章
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
随机推荐
- js中eval()和$.parseJSON()的区别
之前自己一直对ajax不是特别的熟悉,所以一般都很少用这个去写功能,但是最近这个项目中用到了,用ajax异步传数据,json传数据这个时候就需要去解析传过来的数据了,eval()和$.parseJSO ...
- springBoot AOP切面编程
AOP 为 Aspect Oriented Programming 的缩写,意为 面向切面编程.AOP 为spring 中的一个重要内容,它是通过对既有程序定义一个切入点,然后在其前后切入不同的执行内 ...
- 参数化2--CSV Data Set Config 参数化配置
众所周知,在进行接口测试的过程中,需要创建不同的场景(不同条件的输入,来验证不同的入参的返回结果).因而,在日常的自动化接口监控或商品监控等线上监控过程中,需要配置大量的入参来监控接口的返回是否正确. ...
- Linux笔记:vim
文件搜索后显示高亮,即使退出编辑高亮依然存在.使用以下几个方法: 1)指令模式下运行:nohlsearch 2)运行set nohlsearc,可永久关闭搜索高亮 3)搜索任意不存在的字符串
- Excel找出两列相同部分
公式:=IF(COUNTIF($B$2:$B$1036,A6)>0,A2,"") 含义:从B列第2行到b列1036行中和A列第6个相等的返回A6的值,不相等返回空 用法:新建 ...
- PHP页面跳转几种实现方法
转载自冠威博客 [ http://www.guanwei.org/ ]本文链接地址:http://www.guanwei.org/post/PHPnotes/04/php-redirect-metho ...
- 推荐一个好的数据库工具Embarcadero DBArtisan
最近的项目中用到了DB2数据库,由于DB2数据库客户端在操作操作和控制方面不是很方便,如存储过程的编写.后来我们在数据库的操作都转在DBArtisan上了,最新版好像是8.12. 下面介 ...
- 【GLSL教程】(七)逐像素的光照 【转】
http://blog.csdn.net/racehorse/article/details/6662540 逐像素的方向光(Directional Light per Pixel) 这一节将把前面的 ...
- 身份证识别接口编写的JAVA调用示例
此java文章是基本聚合数据证件识别接口来演示,基本HTTP POST请求上传图片并接收JSON数据来处理. 使用前你需要通过 https://www.juhe.cn/docs/api/id/153 ...
- curl库pycurl实例及参数详解
pycurl是功能强大的python的url库,是用c语言写的,速度很快,比urllib和httplib都快. 今天我们来看一下pycurl的用法及参数详解 常用方法: pycurl.Curl() # ...