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.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
/ \
9 20
/ \
15 7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
/ \
2 2
/ \
3 3
/ \
4 4

Return false.

最简单的思路就是建一个height function, 可以计算每个node的height, 然后abs(left_height - right_height) <2, 再recursive 判断即可.

04/20/2019 Update : add one more solution using Divide and conquer. (add a class called returnType)

1. Constraints

1) None => True

2. Ideas

DFS      T: O(n^2)   optimal O(n) S; O(n)

3. Code

1) T: O(n^2)

class Solution:
def isBalance(self, root):
if not root: return True
def height(root):
if not root: return 0
return 1 + max(height(root.left) , height(root.right))
return abs(height(root.left) - height(root.right)) < 2 and self.isBalance(root.left) and self.isBalance(root.right)

2) T: O(n)  S; O(n)

bottom to up, 一旦发现不符合的,就不遍历, 直接返回-1一直到root, 所以不需要每次来计算node的height.

class Solution:
def isBalance(self, root):
def height(root):
if not root: return 0
l, r = height(root.left), height(root.right)
if -1 in [l, r] or abs(l-r) >1: return -1
return 1 + max(l,r)
return height(root) != -1

3) T: O(n) . S: O(n)

class ResultType:
def __init__(self, isBalanced, height):
self.isBalanced = isBalanced
self.height = height class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def helper(root):
if not root:
return returnType(True, 0)
left = helper(root.left)
right = helper(root.right)
if not left.isBalanced or not right.isBalanced or abs(left.height - right.height) > 1:
return ResultType(False, 0)
return ResultType(True, 1 + max(left.height, right.height))
return helper(root).isBalanced

[LeetCode] 110. Balanced Binary Tree_Easy tag: DFS的更多相关文章

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

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

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

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

  3. [LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  4. [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. leetcode 110 Balanced Binary Tree(DFS)

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

  6. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

  7. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

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

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

  9. 110. Balanced Binary Tree (Tree; DFS)

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

随机推荐

  1. 7个简单的Excel技巧,需要的赶紧get起来吧

    1.直观数据图形化 2. Ctrl不连续选择 3. Ctrl+A相连文本框全选 4. 格式刷点击 5. SUM函数求和 6. 自动求和.自动求平均值.自动计数 7. 行.列距调节

  2. 如何通过automator创建自动化备份任务?

    Windows用户对于Task Scheduler应该不陌生,但是到了macOS因该用什么呢?那就是automator,今天因为有一个个人使用需求:备份一个移动硬盘中的文件,但是又不想完全手动.怎么办 ...

  3. 1.7Oob成员变量和局部变量疑难区分

    import java.util.Scanner; public class booleann { private float fWidth; private float fHeight; void ...

  4. Rodrigues Formula

    https://en.wikipedia.org/wiki/Rodrigues%27_formula https://en.wikipedia.org/wiki/Rodrigues%27_rotati ...

  5. nginx之fastcgi和PHP的PHP-FPM

    php-fpm:PHP fastcgi进程管理器 php-fpm的工作模式:1个master进程.多个worker进程(在PHP中worker进程就是php-cgi进程),php-cgi是PHP的解释 ...

  6. Copycat - command

    client.submit(new PutCommand("foo", "Hello world!")); ServerContext connection.h ...

  7. Java代码中获取Json的key值

    测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym ...

  8. tp5, laravel, yii2我该选择哪个

    为什么写这篇文章 我个人有一个技术群,里面学什么框架的都有,经常会有人问 某某功能 在 哪个框架怎么实现,用什么框架实现更好,说道这里我大家讲一个同类型问题的笑话 某女:你能让这个论坛的人都吵起来,我 ...

  9. ubuntu物理机上搭建Kubernetes集群 -- master 配置

    1. 将  kube-apiserver, kube-controller-manager, kube-scheduler,kubectl 拷贝到/usr/bin目录下 2. 安装etcd sudo ...

  10. EF-CodeFirst-域模型配置

    之前说到CodeFirst会使用默认约定从域模型创建数据库,同时也提供了方法重写这些约定;有两种方法可以实现 使用数据注解属性 使用Fluent API 数据注解属性 数据注释是一种简单的基于属性的配 ...