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. js 使用a标签 下载资源

    文档 let data = new Blob(['hello ajanuw'], { type: 'application/text' }) let src = window.URL.createOb ...

  2. djiango web 在进入admin的时候出现'set' object is not reversible错误

    解决方案是在你的urls.py 中 把{ } 改为[] 原因不详 治标不治本,并不能改的东西

  3. 牛客多校10 D Rikka with Prefix Sum 不是数据结构

    https://www.nowcoder.com/acm/contest/148/D 题意: 1e5个数,1e5个操作,操作分为: 1.区间加. 2.整个数列替换为前缀和. 3.区间查询. 查询数小于 ...

  4. 查询自己的apple购买历史

    https://secure1.store.apple.com/cn/order/list

  5. log4j.properties 日志文件的详细配置说明

    一.在一个web 项目中,使用tomcat 启动通常会在控制台输出出现一个警告信息: 通常为未添加 log4j.properties文件的原因. 二.下面以一个普通的maven项目为例说明一下 1. ...

  6. zabbix监控Windows-server

    官网下载客户端 https://www.zabbix.com/download 2.创建自定义文件夹(路径任意),我创建的为C:\zabbix 3.将下载的文件存放至该目录,注意操作系统位数,去对应b ...

  7. ArcGIS API for JavaScript

    以3.14版本为例: 1.部署环境: 下载:https://developers.arcgis.com/downloads/apis-and-sdks?product=javascript# 部署:h ...

  8. [daily][qemu][kvm] 使用virtfs在host与guest之间共享目录

    如题. 之前我使用NFS,NFS会有同步问题.比如编译文件时候的时间同步问题,见前边的文章. 如今,我们使用高级的virtfs: 见:https://www.linux-kvm.org/page/9p ...

  9. Flink - Scheduler

    Job资源分配的过程, 在submitJob中,会生成ExecutionGraph 最终调用到, executionGraph.scheduleForExecution(scheduler) 接着,E ...

  10. cocos2dx 粒子系统

    参考文献: 1.http://blog.csdn.net/aa4790139/article/details/8126525 2.https://code.google.com/p/cocos2d-w ...