平衡二叉树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 ...
随机推荐
- 1016 Phone Bills (25 分)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following rul ...
- CART、GradientBoost
转载:https://blog.csdn.net/niuniuyuh/article/details/76922210 论文:http://pdfs.semanticscholar.org/0d97/ ...
- 管理oracle 11g RAC 常用命令
1).检查集群状态: [grid@rac02 ~]$ crsctl check cluster CRS-4537: Cluster Ready Services is online CRS-4529: ...
- Spark SQL Hive Support Demo
前提: 1.spark1.0的包编译时指定支持hive:./make-distribution.sh --hadoop 2.3.0-cdh5.0.0 --with-yarn --with-hive - ...
- 在 Linux 下使用mdadm创建 RAID 5
在 RAID 5 中,数据条带化后存储在分布式奇偶校验的多个磁盘上.分布式奇偶校验的条带化意味着它将奇偶校验信息和条带化数据分布在多个磁盘上,这样会有很好的数据冗余. 在 Linux 中配置 RAID ...
- Kafka 基本原理
Kafka 基本原理 来源:阿凡卢 , www.cnblogs.com/luxiaoxun/p/5492646.html 简介 Apache Kafka是分布式发布-订阅消息系统.它最初由Link ...
- word 标题映射错乱
关闭Document Map,退出word 再次打开
- Select Case 的简单实现
FOR %%f in (1,3,5,7,) do if errorlevel == %%f goto CASE%%f echo ERROR ...
- Mysql 2条记录 差值计算
1 表结构 2: 其实 是2个相同的 表根据rownum= rownum-1 来计算,所以先了解单个表的查询 附上SQL: #查询出1天的数据升序 ) as rownum, info.equipme ...
- Python之关系字段
参考:https://blog.csdn.net/pugongying1988/article/details/72870264 关系字段:一对一,多对一,多对多 一对一: 现在有很多一对一辅导班, ...