首页 技术 正文
技术 2022年11月23日
0 收藏 452 点赞 4,067 浏览 1871 个字

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
LeetCode: Balanced Binary Tree  解题报告

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781