Leetcode 笔记 110 - Balanced Binary Tree
题目链接: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的更多相关文章
- 【一天一道LeetCode】#110. Balanced Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】110. Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
随机推荐
- StringMVC 中如何做数据校验
步骤一:引入四个jar包 步骤二:注册类型转换器 <context:component-scan base-package="cn.happy.controller"> ...
- BootStrap_02之全局样式及组件
1.BootStrap指定的四种屏幕尺寸: ①超大PC屏幕--lg(large):w>=1200px: ②中等PC屏幕--md(medium):1200px>w>=992px: ③P ...
- 我为NET狂官方面试题-数据库篇答案
题目:http://www.cnblogs.com/dunitian/p/6028838.html 汇总:http://www.cnblogs.com/dunitian/p/5977425.html ...
- 分布式系列文章——从ACID到CAP/BASE
事务 事务的定义: 事务(Transaction)是由一系列对系统中数据进行访问与更新的操作所组成的一个程序执行逻辑单元(Unit),狭义上的事务特指数据库事务. 事务的作用: 当多个应用程序并发访问 ...
- ASP.NET MVC开发:Web项目开发必备知识点
最近加班加点完成一个Web项目,使用Asp.net MVC开发.很久以前接触的Asp.net开发还是Aspx形式,什么Razor引擎,什么MVC还是这次开发才明白,可以算是新手. 对新手而言,那进行A ...
- ES6的一些常用特性
由于公司的前端业务全部基于ES6开发,于是给自己开个小灶补补ES6的一些常用特性.原来打算花两天学习ES6的,结果花了3天才勉强过了一遍阮老师的ES6标准入门(水好深,ES6没学好ES7又来了...) ...
- 28个你必须知道的HTML5的新特性,技巧以及技术
崭新新的页面布局 传统的: HTML5: 1. 新的Doctype 尽管使用<!DOCTYPE html>,即使浏览器不懂这句话也会按照标准模式去渲染 2. Figure元素 用<f ...
- eclipse — 导入android项目后识别成java项目的问题及解决
最近在eclipse导入android项目的时候遇到了奇葩问题,再此记录 遇到的问题就是:将完好的android项目导入到eclipse的时候,原本这是一个很容易的事情,但是导入成功后发现,,,靠ec ...
- Android Studio切换为eclipse的快捷键之后还是有区别的部分快捷键
Android Studio Eclipse 把代码提示换成了Class Name Completion, 快捷键是Ctrl+Alt+Space(空格键). 代码提示快捷键Alt+/, ...
- MySQL:Fabric 安装
MySQL Fabric安装 MySQL Fabric是Oracle提供的用于辅助进行ha\sharding的工具,它的基本架构: 从上面看出,借助于Fabric, 可以搭建 HA 集群.Sharin ...