题目链接:Balanced Binary Tree | LeetCode OJ

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.

Tags: Depth-first Search

分析

同样是很基础的深度优先遍历题目,只要在遍历时取得左右子树的深度,对比是否相差超过1就可以得出结果,需要考虑的技巧是怎么在发现不平衡之后,最迅速的返回结果,不做多余的计算。

有可能出现的问题是先写一个Helper方法获得结点到最下层叶子结点的深度,然后在深度优先遍历中每次调用这个方法来对比深度。这是不必要的,获取深度本身就是用深度优先遍历实现的,一边遍历一边计算深度就OK。

示例

class Solution:
# @param root, a tree node
# @return a boolean
def isBalanced(self, root):
return self.getBalanceHeight(root) != -1 # @param root, a tree node
# @return a int, if the root is balanced return height, or return -1
def getBalanceHeight(self, root):
if root is None:
return 0; leftHeight = self.getBalanceHeight(root.left)
# if left child tree is not balanced, return -1 directly to stop recursion
if leftHeight < 0:
return -1 rightHeight = self.getBalanceHeight(root.right)
# if right child tree is not balanced, return -1 directly to stop recursion
if rightHeight < 0:
return -1 if math.fabs(leftHeight - rightHeight) > 1:
return -1
return max(leftHeight, rightHeight) + 1

Leetcode 笔记系列的Python代码共享在https://github.com/wizcabbit/leetcode.solution

扩展

看到了有童鞋的解法类似与:

def getHeight(self, root):
if root == None:
return 0
left_height, right_height = self.getHeight(root.left), self.getHeight(root.right)
if left_height < 0 or right_height < 0 or math.fabs(left_height - right_height) > 1:
return -1
return max(left_height, right_height) + 1

这个解法是没问题可以Accpeted的,但是我在分别计算出leftHeight和rightHeight之后,立刻检测了一下其深度是否为-1,如果左或右子树的深度返回-1,证明子树不是Balanced的,可以不再计算其他子树的深度,当前结点也返回-1.这样可以在发现不平衡的子树后,立刻终止遍历,比上面的算法稍快一些。

另外,至少在Python中,使用和高度值同为int的-1表示子树不平衡,比使用None或者False来表示要节省时间,如果使用None而不是-1,跑完所有的Test Case大概会多用1/4的时间。

Leetcode 笔记 110 - Balanced Binary Tree的更多相关文章

  1. 【一天一道LeetCode】#110. Balanced Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. LeetCode OJ 110. Balanced Binary Tree

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

  3. 【LeetCode】110. Balanced Binary Tree

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

  4. 110.Balanced Binary Tree Leetcode解题笔记

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

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

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

  6. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

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

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

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

随机推荐

  1. Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制

    将通用的序列号生成器库 从SQL Server迁移到Mysql 遇到的一个问题,就是TimeStamp/RowVersion并发控制类型在非Microsoft SQL Server数据库中的实现.SQ ...

  2. nodejs进阶(1)—输出hello world

    下面将带领大家一步步学习nodejs,知道怎么使用nodejs搭建服务器,响应get/post请求,连接数据库等. 搭建服务器页面输出hello world var  http  =  require ...

  3. Hawk 4.7 单步调试

    单步调试的意义 已经编写的工作流,可能会因为某些外界环境的变化而出错,此时需要排除错误,我们可以使用单步调试. 单步调试的本质,相当于只使用前n个模块,这样就能看到每个步骤下,流的改变. 例子 还是上 ...

  4. H3 BPM初次安装常见错误详解1-4

    错误1: 首次安装完成无法访问,效果如下. 错误原因:没有配置IIS. 解决方法: 控制面板-程序-打开或关闭Windows功能,选择internet信息服务. 因为安装的时候没有没有iis,所以程序 ...

  5. 一切从“简”,解放IT运维人员

    运维人的神技 运维既是个技术活儿也是个苦差事,而运维人员被期望有着无限的技能:主机.存储.网络.操作系统样样精通,而且还要会写SQL.shell.开发语言java..net.python等等,对业务更 ...

  6. 在centos7(EL7.3 即 kernel-3.10.0-514.X )上安装BCM4312无线网卡驱动要注意的问题

    我新装的centos7主机无法使用里面自带的网卡,查询后发现网卡型号为BCM4312.我在看资料安装的过程中遇到了些问题,纠结了好久,现在分享下要注意的点,为后来的遇到同样问题的人提供点帮助.现在开始 ...

  7. 第14章 Linux启动管理(3)_系统修复模式

    3. 系统修复模式 3.1 单用户模式 (1)在grub界面中选择第2项,并按"e键"进入编辑.并在"-quiet"后面加入" 1",即&q ...

  8. mono for android Json 上传文件

    void button_Click(object sender, EventArgs e) { string Url = "上传地址,服务器端负责接收"; byte[] fbyte ...

  9. 让ASP.NET接受有“潜在危险”的提交

    什么是有“潜在危险”的提交?马上动手写个简单的例子:   用Visual Studio创建一个空白的ASP.NET MVC程序,一切默认即可,添加一个空白的HomeController,增加一个Ind ...

  10. Netty5使用自签证书实现SSL安全连接

    这次使用的Netty是最新的5.0 Alpha2版本,下载地址是:http://dl.bintray.com/netty/downloads/netty-5.0.0.Alpha2.tar.bz2,发布 ...