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. hdu2594 Simpsons' Hidden Talents【next数组应用】

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  2. [No000010F]Git8/9-使用GitHub

    我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...

  3. elasticsearch ingest node and docker-cluster---quey using sql]

    es-docker-cluster https://stefanprodan.com/2016/elasticsearch-cluster-with-docker/ https://github.co ...

  4. [Day1]常用Dos命令,Java相关描述及基础

    1.常用的DOS命令 (1)返回上一级目录:cd.. (2)返回盘符根目录:cd\ (3)切换当前盘符:   盘符: (4)进入文件夹:       cd 文件路径 (5)展示当前目录下的所有内容:D ...

  5. day 0313函数的初识

    1.函数的定义: 定义:def 关键词开头,空格之后接函数名和圆括号(),还有最后一个‘:’ def是固定的,定义函数的关键字. 空格-是为了将关键字和函数名分开,必须有的. 函数名:只能包括字符串, ...

  6. 可变数组(PLSQL)

    可变数组 可变数组与嵌套表相似,也是一种集合.一个可变数组是对象的一个集合,其中每个对象都具有相同的数据类型.可变数组的大小由创建时决定.在表中建立可变数组后,可变数组在主表中作为一个列对待.从概念上 ...

  7. SQL instr()函数的格式

    格式一:instr( string1, string2 )    /   instr(源字符串, 目标字符串) 格式二:instr( string1, string2 [, start_positio ...

  8. 内核block机制

    内核版本:linux2.6.22.6 硬件平台:JZ2440 驱动源码 block_ipc_poll_key_int_drv.c : #include <linux/module.h> # ...

  9. sqlserver配置允许快照隔离

    ALTER DATABASE TustenaOS SET ALLOW_SNAPSHOT_ISOLATION ON

  10. VS Code 创建代码段 Snippets

    菜单:文件 -> 首选项 -> 用户代码片断 打开User Snippets菜单: 选择C#: 然后把里面注释的文字留下, 复制其中那段代码并修改称自己的代码段: "Create ...