LeetCode: Balanced Binary Tree 解题报告
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.
Show Tags
SOLUTION 1:
使用inner Class来解决
// Solution 1:
public boolean isBalanced1(TreeNode root) {
return dfs(root).isBalanced;
} // bug 1: inner class is like: "public class ReturnType {", no ()
public class ReturnType {
boolean isBalanced;
int depth; ReturnType(int depth, boolean isBalanced) {
this.depth = depth;
this.isBalanced = isBalanced;
}
} public ReturnType dfs(TreeNode root) {
ReturnType ret = new ReturnType(0, true); if (root == null) {
return ret;
} ReturnType left = dfs(root.left);
ReturnType right = dfs(root.right); ret.isBalanced = left.isBalanced
&& right.isBalanced
&& Math.abs(left.depth - right.depth) <= 1; // bug 2: remember to add 1( the root depth )
ret.depth = Math.max(left.depth, right.depth) + 1; return ret;
}
SOLUTION 2:
将 get depth函数提出
// Solution 2:
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
} return isBalanced(root.left) && isBalanced(root.right)
&& Math.abs(getDepth(root.left) - getDepth(root.right)) <= 1;
} public int getDepth(TreeNode root) {
if (root == null) {
return 0;
} return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
}
SOLUTION 3:
leetcode又加强了数据,solution 2对于一条单链过不了了。所以主页君加了一点优化,当检测到某个子节点为null时,求另一个子树的depth时,及时退出,这
样就不会产生getdepth太深的问题:
// Solution 2:
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
} boolean cut = false;
if (root.right == null || root.left == null) {
cut = true;
} return isBalanced(root.left) && isBalanced(root.right)
&& Math.abs(getDepth(root.left, cut) - getDepth(root.right, cut)) <= ;
} public int getDepth(TreeNode root, boolean cut) {
if (root == null) {
return -;
} if (cut && (root.left != null || root.right != null)) {
// if another tree is not deep, just cut and return fast.
// Improve the performance.
return ;
} return + Math.max(getDepth(root.left, false), getDepth(root.right, false));
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/IsBalanced.java
LeetCode: Balanced Binary Tree 解题报告的更多相关文章
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【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 参考资料 日期 题目 ...
随机推荐
- C++11开发中的Atomic原子操作
C++11开发中的Atomic原子操作 Nicol的博客铭 原文 https://taozj.org/2016/09/C-11%E5%BC%80%E5%8F%91%E4%B8%AD%E7%9A%84 ...
- Linux下面安装RabbitMQ Cluster
安装rabbitmq cluster: 设置 Erlang Cookie安装完RabbitMQ之后,在第一台机器上面启动RabbitMQ,然后在停止.复制node1上的/var/lib/rabbitm ...
- win7怎么快速截取图片
点击开始--运行或者winkey + r 键直接进入运行. 2 在输入框输入snippingtool,点击确定. 3 这就找到截图工具,如图. END 方法/步骤2 进入c盘--Windows-- ...
- 转 mongodb win7 32位系统安装以及配置
mongodb win7 32位系统安装以及配置 今天安装 win7 32位系统 mongodb 费了好大劲..记录一下,希望相同的同学可以少踩点坑. 1.安装 我安装的是3.2.4 地址:ht ...
- python packages prebuild for windows
python prebuild / precompiled packages for windows by uci edu 在python windows环境下作业,有时候会碰上一些无厘头的问 ...
- 如何提升RDS响应速度
如果采用短连接的方式连接RDS,请在与RDS相连的云服务器上执行以下命令来提升RDS响应速度.(注:云服务器要有公网带宽) 1.安装nscd 云服务器上执行命令 Ubuntu系统执行:apt-get ...
- iOS 9 学习系列: Xcode Code Coverage
Code coverage 是一个计算你的单元測试覆盖率的工具. 高水平的覆盖给你的单元測试带来信心.也表明你的应用被彻底的測试过了. 你可能写了几千个单元測试,但假设覆盖率不高.那么你写的这套測试可 ...
- [转]Filter 过滤器
1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...
- Java 8 – Convert Instant to LocalDateTime
Java 8 examples to show you how to convert from Instant to LocalDateTime 1. Instant -> LocalDateT ...
- DataGridView控件使用大全说明-各种常用操作与高级操作
DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特 ...