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 参考资料 日期 题目 ...
随机推荐
- memcached完全剖析--1. memcached的基础
翻译一篇技术评论社的文章,是讲memcached的连载.fcicq同学说这个东西很有用,希望大家喜欢. 发表日:2008/7/2 作者:长野雅广(Masahiro Nagano) 原文链接:http: ...
- java生成PDF,各种格式、样式、水印都有
代码中有两处需要图片,请自行替换. 一个是水印.一个是手指. 需要的JAR包链接:http://download.csdn.net/detail/justinytsoft/9688893 下面是预览: ...
- 【SqlServer】SqlServer索引的创建、查看、删除
索引加快检索表中数据的方法,它对数据表中一个或者多个列的值进行结构排序,是数据库中一个非常有用的对象. 索引的创建 #1使用企业管理器创建 启动企业管理器--选择数据库------选在要创建索引的表- ...
- hashCode和identityHashCode 的关系
1:首先看一下JDk API的观点 1-1:hashCode方法相关 1-2:identityHashCode()方法相关 2:此例的核心程序,对应的观点在注释中已经有所说明,请自己也动手实验一下看看 ...
- Java与C++Socket通讯注意事项
c++与java进行socket通信时注意事项 原文链接: http://my.oschina.net/ypimgt/blog/106439 因为java发送的都是网络字节序(big-endium), ...
- C#基础第六天-作业-利用面向对象的思想去实现名片
1.利用面向对象的思想去实现: (增加,修改,删除,查询,查询全部)需求:根据人名去(删除/查询).指定列:姓名,年龄,性别,爱好,电话. 本系列教程: C#基础总结之八面向对象知识点总结-继承与多态 ...
- 独立的android开发者开发app如何盈利?
对立android开发者开发app如何盈利?android开发日益兴隆,随着google的大力推广和技术及其android培训的支持,android个人开发者或者android独立开发者也都匆匆欲动加 ...
- spring 项目中使用 hibernate validator验证输入参数
1 hibernate validator 官方文档:https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_si ...
- 讲讲python“=”运算符上的优雅语法
心路历程: 之前学linux,虽然学的行算不错,不过总感觉差了点什么,自己找不到也说不出来:直到有一天我看到别人mount上了一个普通文件: 当时给我的感觉这太不可思议了,这个文件又不是块设备:后来脑 ...
- 微信小程序-实现分享(带参数)
微信小程序分享功能的实现方法有两种: 第一种 在page.js中实现onShareAppMessage,便可在小程序右上角选择分享该页面 onShareAppMessage: function () ...