平衡二叉树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.
[思维问题]:
以为要用traverse一直判断,结果发现是没有读题。
[一句话思路]:
定义新的类 来帮助判断。(符合工业规范)
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
最后的最大深度是left right中最大的,再+1
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[总结]:
[复杂度]:Time complexity: O(n) Space complexity: O(lgn)
[英文数据结构,为什么不用别的数据结构]:
定义新的类 来帮助判断。(符合工业规范)
[其他解法]:
分治法
[Follow Up]:
[LC给出的题目变变变]:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
class ResultType {
boolean isBalanced;
int maxDepth;
ResultType(boolean isBalanced,int maxDepth) {
this.isBalanced = isBalanced;
this.maxDepth = maxDepth;
}
}; public boolean isBalanced(TreeNode root) {
return helper(root).isBalanced;
} private ResultType helper (TreeNode root) {
if (root == null) {
return new ResultType(true, 0);
} ResultType left = helper(root.left);
ResultType right = helper(root.right); if (!left.isBalanced || !right.isBalanced) {
return new ResultType(false, - 1);
} else if (Math.abs(left.maxDepth - right.maxDepth) > 1) {
return new ResultType(false, - 1);
}
else {
return new ResultType(true, Math.max(left.maxDepth,right.maxDepth) + 1);
}
}
}
平衡二叉树Balanced Binary Tree的更多相关文章
- 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树
平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- [Swift]LeetCode110. 平衡二叉树 | 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)
问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...
- 平衡二叉树(Balanced Binary Tree)
平衡二叉树(Balanced Binary Tree)/AVL树:
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
随机推荐
- IT职业发展攻略(技术仅是工具而已)
时光飞逝,我事业中第一个十年就快结束了.在这十年里,让我收获了很多,今天想与大家分享一下,我在 IT 职场方面的一些个人经验,不一定对每个人都实用,请大家仅作参考吧. 大家既然都是做技术的,那我们不妨 ...
- LightGBM优势总结
效率和内存上的提升 1) 在训练决策树计算切分点的增益时,xgboost采用预排序,即需要对每个样本的切分位置都要计算一遍,所以时间复杂度是O(#data). 而LightGBM则是将样本离散化为直方 ...
- 样本稳定指数PSI
信用评定等级划分之后需要对评级的划分做出评价,分析这样的评级划分结果是否具有实用价值,即分析样本分布的稳定程度.样本分布稳定,则信用评定等级划分结果的实用价值就高.采用样本稳定指数( PSI )检验样 ...
- golang web框架 beego 学习 (二) router and controller
1 Router和Controller的常用配置 beego.Router("/user/admin", &controllers.UserController{}) // ...
- java基础:关于java流与文件操作
1.描述:流是字节数据或字符数据序列.Java采用输入流对象和输出流对象来支持程序对数据的输入和输出.输入流对象提供了数据从源点流向程序的管道,程序可以从输入流对象读取数据:输出流对象提供了数据从程序 ...
- ByteCache
private static class ByteCache { private ByteCache(){} //256个元素,用于缓存-128到127 static final Byte cache ...
- 34. CentOS-6.3安装配置Apache2.2.6
安装说明 安装环境:CentOS-6.3安装方式:源码编译安装 软件:httpd-2.2.6.tar.gz | pcre-8.32.tar.gz | apr-1.4.6.tar.gz | apr-u ...
- Simple2D-26 Simple2D 最后的工作,开发结束
开始的时候打算将 Simple2D 做成一个库的,但现在没有那个功夫了. 要渲染顶点数据,就必须将渲染函数放置到 glClear( ) 函数和 SwapBuffers( ) 函数之间,但又不希望开发时 ...
- autolayout UILabel 设置最大宽度
label1.preferredMaxLayoutWidth = 100: label1.numberOfLines = 0; //自适应行数
- RISC处理器
RISC(精简指令集算法)处理器是经过硬件的精简只执行很有限的最常用的那部分指令的处理器.因为通过研究发现,只有 大约 20%的指令是最常用的,把处理器能执行的指令数目减少到 最低限度,对它们的执行 ...