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. gedit脚本

    明天ctsc,赶紧学了一下gedit的配置 以下假设你只在/home/zzq下写代码(用户名自己改) 首先在/home/zzq下建一个runner.sh,内容如下: #!/bin/bash echo ...

  2. adb 常用命令

    1.adb shell dumpsys SurfaceFlinger 查看window层结构

  3. 微软职位内部推荐-Software Development Engineer

    微软近期Open的职位: Job Title: Software Development Engineer Work Location: Suzhou, China The Office 365 Co ...

  4. Cordova - 常用的插件汇总(附插件的安装、查询、更新、删除等命令)

    Hybrid应用比web应用强大之处在于可以使运行在容器中的web内容访问 native APIs.Cordova 提供了许多插件用于调用移动设备上的API. 一,插件相关常用命令   1,查看所有已 ...

  5. pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat

    pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat Windows7下pip安装包报错:Microso ...

  6. 端口扫描base

    #coding:utf8 import os import socket import sys def IsOpen(ip,port): s = socket.socket(socket.AF_INE ...

  7. scrapy 保存到 sqlite3

    scrapy 爬取到结果后,将结果保存到 sqlite3,有两种方式 item Pipeline Feed Exporter 方式一 使用 item Pipeline 有三个步骤 文件 pipelin ...

  8. 开发备忘:AngularJS Syntax error, unrecognized expression in template file

    在写基于Angular的项目过程中,运行 grunt test的时候,一直给我蹦出这个错误,导致我的test一直跑不过,怎么试都是失败,经过重复排查,发现是因为template file中的html元 ...

  9. python 之禅

    想要真正深入了解一门语言,需要用心去感受.下面是python之禅,python的设计哲学,对于编程很有指导意义.(翻译部分摘自网络,同时自己有一些更改) >>> import thi ...

  10. Hibernate原生SQL映射MySQL的CHAR(n)类型到String时出错

    今天在用Hibernate通过原生SQL和ResultTransformer映射时,出现数据类型不匹配的错误.但是通过Entity映射,没有问题.在网上找了好多答案,终于解决了. 核心代码: Stri ...