平衡二叉树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 ...
随机推荐
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- java编译器
编译: .java变成.class 前端编译 Sun javac Eclipse ECJ .class变成机器码 运行期编译 等HostSpot VM c1,c2 .jav ...
- PHP mysqli_fetch_object() 函数实例讲解
定义和用法 mysqli_fetch_object() 函数从结果集中取得当前行,并作为对象返回. 注释:该函数返回的字段名是区分大小写的. 语法 mysqli_fetch_object(result ...
- openssl - cookbook
1.openssl 2.Testing 3.Best Practices last 1.openssl 1.1.Key and Cerificate Management Run a web serv ...
- mysql5.7.13 使用笔记
社区版下载地址:https://dev.mysql.com/downloads/mysql/ 安装:http://www.linuxidc.com/Linux/2016-04/130414.htm ...
- openlayers-热地图加载(完整版及代码)
//地圖加載function mapInit(data){ //底图// var raster = new ol.layer.Tile({// source: new ol.source.Stamen ...
- win10下装的ubuntu14.04双系统,ubuntu系统访问win10磁盘问题
参考:https://blog.csdn.net/u010426270/article/details/52420231 ubuntu下 解决方法: 1. 在终端输入如下命令,查看分区挂载情况 sud ...
- leetcode342
public class Solution { public bool IsPowerOfFour(int num) { ) && ((num & (num - )) == ) ...
- IIS 更新EXE文件
IIS 更新EXE文件 MIME,add,文件扩展名带不带.都可以,会自动加上.的 文件扩展名:.exe MIME类型:application/octet-stream .ini文件
- as3 连接mysql
http://www.cnblogs.com/yili16438/archive/2011/04/23/2025936.html