[抄题]:

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的更多相关文章

  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. [UE4]Acotr

    任何能被放在关卡中的对象都是Actor Tick是每帧都会调用的事件

  2. 阿里云上部署tomcat启动后,通过http不能访问

    原因是因为阿里为了安全设置了安全组策略,必须我们授权的端口,其他计算机才能通过http访问 设置流程: 点击安全组 再点击:配置规则 然后点击:添加安全组规则 开始配置:划红线的必写,授权对象:0.0 ...

  3. 基于sklearn的 BaseEstimator开发接口:模型融合Stacking

    转载:https://github.com/LearningFromBest/CMB-credit-card-department-prediction-of-purchasing-behavior- ...

  4. 批处理-通过mono把c#编译成dll

    ::copyright@cjy @echo off ::mcs.exe address set addrMcs=D:\Program Files\Unity\Editor\Data\MonoBleed ...

  5. 小朋友学Python(1):Python简介与编程环境搭建

    一.Python简介 不死Java,不朽C/C++,新贵Python. Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰 ...

  6. CSS个人笔记

    1. CSS盒模型 1.1 控制元素尺寸属性 1.1.1 box-sizing: 改变元素应用的尺寸规则 当设置元素尺寸宽度为固定值时(eg: 100px), 其实是元素内容区域的宽度为100px, ...

  7. Redis-基本数据类型与内部存储结构

    1-概览 Redis是典型的Key-Value类型数据库,Key为字符类型,Value的类型常用的为五种类型:String.Hash .List . Set . Ordered Set 2- Redi ...

  8. QT按键(Qbutton)改变颜色

    第一种是按键上面的字颜色的改变:   ui->motor1->setStyleSheet("color: red"); 效果: 第二种是背景改变: ui->mot ...

  9. python3一个简单的网页抓取

    都是学PYTHON.怎么学都是学,按照基础学也好,按照例子增加印象也好,反正都是学 import urllib import urllib.request data={} data['word']=' ...

  10. Xeon Phi 编程备忘

    ▶ 闲鱼的 Xeon Phi 3120A 配办公室的新 Xeon 服务器,记录一下环境安装过程. ● 原本尝试搭 Ubuntu 服务器,参考[https://software.intel.com/en ...