[抄题]:

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给出的题目变变变]:

  1. /**
  2. * Definition of TreeNode:
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left, right;
  6. * public TreeNode(int val) {
  7. * this.val = val;
  8. * this.left = this.right = null;
  9. * }
  10. * }
  11. */
  12.  
  13. public class Solution {
  14. /*
  15. * @param root: The root of binary tree.
  16. * @return: True if this Binary tree is Balanced, or false.
  17. */
  18. class ResultType {
  19. boolean isBalanced;
  20. int maxDepth;
  21. ResultType(boolean isBalanced,int maxDepth) {
  22. this.isBalanced = isBalanced;
  23. this.maxDepth = maxDepth;
  24. }
  25. };
  26.  
  27. public boolean isBalanced(TreeNode root) {
  28. return helper(root).isBalanced;
  29. }
  30.  
  31. private ResultType helper (TreeNode root) {
  32. if (root == null) {
  33. return new ResultType(true, 0);
  34. }
  35.  
  36. ResultType left = helper(root.left);
  37. ResultType right = helper(root.right);
  38.  
  39. if (!left.isBalanced || !right.isBalanced) {
  40. return new ResultType(false, - 1);
  41. }
  42.  
  43. else if (Math.abs(left.maxDepth - right.maxDepth) > 1) {
  44. return new ResultType(false, - 1);
  45. }
  46. else {
  47. return new ResultType(true, Math.max(left.maxDepth,right.maxDepth) + 1);
  48. }
  49. }
  50. }

平衡二叉树Balanced Binary Tree的更多相关文章

  1. 平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树

    平衡二叉树(Balanced Binary Tree 或 Height-Balanced Tree)又称AVL树 (a)和(b)都是排序二叉树,但是查找(b)的93节点就需要查找6次,查找(a)的93 ...

  2. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  3. [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. [LeetCode 110] - 平衡二叉树 (Balanced Binary Tree)

    问题 给出一棵二叉树,判断它是否在高度上是平衡的. 对于本问题,高度上平衡的二叉树定义为:每个节点的两棵子树的深度差永远不大于1的一棵二叉树. 初始思路 根据定义,思路应该比较直接:递归计算每个节点左 ...

  5. [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 ...

  6. 平衡二叉树(Balanced Binary Tree)

    平衡二叉树(Balanced Binary Tree)/AVL树:

  7. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  8. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  9. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

随机推荐

  1. 搭建GlusterFS文件系统

    (1)环境准备 创建两个虚拟机配置如下 把仅主机第二张网卡配置如下: GlusterFS1 GlusterFS2 上传文件到opt目录下 文件内容如下 (2)GlusterFS安装配置 1.安装Glu ...

  2. vs2013错误解决方法

    1.cannot determine the location of the vs common tools folder 打开"VS2013开发人员命令提示后",上面提示&quo ...

  3. 【Codeforces】CF 8 C Looking for Order(状压dp)

    题目 传送门:QWQ 分析 这种题不会做 吃枣药丸..... 想到状压已经经过的点. 然后更新时枚举两个点加进去. 复杂度$  {O(2^n \times n^2)}$. 凉凉. 真正的做法是每一个状 ...

  4. Spark SQL Hive Support Demo

    前提: 1.spark1.0的包编译时指定支持hive:./make-distribution.sh --hadoop 2.3.0-cdh5.0.0 --with-yarn --with-hive - ...

  5. CSS宽度高度的百分比取值基于谁

    width=num% , height=num% 基于以下几点 1. 若元素不存在定位: 则基于直接父元素的宽高度 2. 若元素存在定位 且 定位为 relative, 则也基于直接父元素的宽高度 3 ...

  6. jmeter+maven 的简单使用 记录(Windows环境)

    1.手动创建maven工程目录结构,maven对目录结构要求比较严格(pom.xml文件一定要放在根目录下) Maven --src --main --test --jmeter --resource ...

  7. CUDA C Programming Guide 在线教程学习笔记 Part 1

    1. 简介 2. 编程模型 ▶ SM version 指的是硬件构架和特性,CUDA version 指的是软件平台版本. 3. 编程接口.参考 http://chenrudan.github.io/ ...

  8. phone手机比较

    phone 15年5月3大厂商发布的高端机器 高颜值手机 皓月银 http://www.vmall.com/product/1983.html 冰川白 http://www.vmall.com/pro ...

  9. 机器学习入门-概率阈值的逻辑回归对准确度和召回率的影响 lr.predict_proba(获得预测样本的概率值)

    1.lr.predict_proba(under_text_x)  获得的是正负的概率值 在sklearn逻辑回归的计算过程中,使用的是大于0.5的是正值,小于0.5的是负值,我们使用使用不同的概率结 ...

  10. 可视化库-seaborn-回归分析绘图(第五天)

    1. sns.regplot() 和 sns.lmplot() 绘制回归曲线 import numpy as np import pandas as pd from scipy import stat ...