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.

 def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
factor = abs(self.level(root.left) - self.level(root.right))
return factor < 2 and self.isBalanced(root.right) and self.isBalanced(root.left) def level(self, root):
if not root:
return 0
return max(self.level(root.left), self.level(root.right)) + 1
这里的方法使用了递归,每个subtree会多次计算level。虽然可以通过OJ, 但是可以使用DP提高效率。 建立一个Hash table, root作为key, level函数的值作为value。
 d = {}
class Solution(object):
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
factor = abs(self.level(root.left) - self.level(root.right))
return factor < 2 and self.isBalanced(root.left) and self.isBalanced(root.right) def level(self, root):
if not root:
return 0 if root in d:
return d[root]
else:
d[root] = max(self.level(root.left), self.level(root.right)) + 1
return d[root]

 

Leetcode 110. Balanced Binary Tree的更多相关文章

  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] 110. 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. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

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

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

  6. leetcode 110 Balanced Binary Tree ----- java

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

  7. Java [Leetcode 110]Balanced Binary Tree

    题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...

  8. [LeetCode] 110. Balanced Binary Tree 解题思路

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

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

    翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...

随机推荐

  1. 使用 Fastlane 实现 IOS 持续集成

    简介 持续集成是个“一次配置长期受益”的工作.但很多小公司都没有.以前在做Windows开发配置感觉简单一些,这次配置iOS的,感觉步骤还挺多.整理出来,分享给大家,不正确的地方请及时指正. 本文主要 ...

  2. SQL使用开窗函数与CTE查询每月销售额的前几名

    WITH tagTab AS( SELECT YearMonth, pm=RANK() OVER(PARTITION BY YearMonth ORDER BY amount DESC) FROM S ...

  3. C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 标准省市县数据的公司选择窗口实现

    若全国各地有上千个分公司,加盟店,网点,那就需要一个友善的选择分公司的功能,得有标准的全国省市县的划分数据.这样有了牢靠的基础数据后,才能开发程序得心应手了.当习惯了开发一个公司内部系统时,全国性大公 ...

  4. gocode+auto-complete搭建emacs的go语言自动补全功能

    上篇随笔记录了在emacs中使用go-mode和goflymake搭建了go语言的简单编程环境(推送门),今天来记录一下使用gocode+auto-complete配置emacs中go语言的自动补全功 ...

  5. Excel导入导出,通过datatable转存(篇一)

    //导入数据 public ActionResult ExpressInfoImport() { var ptcp = new BaseResponse() { DoFlag = true, DoRe ...

  6. MATLAB中的set函数

    1.MATLAB给每种对象的每一个属性规定了一个名字,称为属性名,而属性名的取值成为属性值.例如,LineStyle是曲线对象的一个属性名,它的值决定着线型,取值可以是'-' .':'.'-.'.'- ...

  7. 如何设置div高度为100%

    div高度通常都是固定值,直接将div高度设为100%是无效的,那么如何设置才能有效呢? 直接给div设置height:100%即可,无效的原因一定是父元素的高度为0,最常见的就是给body的直接元素 ...

  8. Go加密解密之DES

    一.DES简介 DES(Data Encryption Standard)是对称加密算法,也就是加密和解密用相同的密钥.其入口参数有三个:key.data.mode.key为加密解密使用的密钥,dat ...

  9. iOS -- 上传多张图片 后台(PHP)代码和上传一张的一样

    // 上传多张图片 - (void)send { // 设置初始记录量为0 self.count = 0; self.upcount = 0; // 设置初始值为NO self.isUploadPic ...

  10. SharePoint Backup

    这里主要介绍使用admin center直接backup: 1.浏览器进入管理中心,选择备份: 2.按需要选择需要备份的内容 3.选择备份位置,然后等待服务器备份完成(windows explore中 ...