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 ...
随机推荐
- Web性能优化:图片优化
程序员都是懒孩子,想直接看自动优化的点:传送门 我自己的Blog:http://cabbit.me/web-image-optimization/ HTTP Archieve有个统计,图片内容已经占到 ...
- ABP源码分析一:整体项目结构及目录
ABP是一套非常优秀的web应用程序架构,适合用来搭建集中式架构的web应用程序. 整个Abp的Infrastructure是以Abp这个package为核心模块(core)+15个模块(module ...
- KV存储系统
现在的KV存储系统都是分布式的,首先介绍Zookeeper——针对大型分布式系统的高可靠的协调系统. 开发分布式系统是件很困难的事情,其中的困难主要体现在分布式系统的“部分失败”.“部分失败”是指信息 ...
- Word/Excel 在线预览
前言 近日项目中做到一个功能,需要上传附件后能够在线预览.之前也没做过这类似的,于是乎就查找了相关资料,.net实现Office文件预览大概有这几种方式: ① 使用Microsoft的Office组件 ...
- python+uwsgi导致redis无法长链接引起性能下降问题记录
今天在部署python代码到预生产环境时,web站老是出现redis链接未初始化,无法连接到服务的提示,比对了一下开发环境与测试环境代码,完全一致,然后就是查看各种日志,排查了半天也没有查明是什么原因 ...
- Xamarin+Prism小试牛刀:定制跨平台Outlook邮箱应用(后续)
在[Xamarin+Prism小试牛刀:定制跨平台Outlook邮箱应用]里面提到了Microsoft 身份认证,其实这也是一大块需要注意的地方,特作为后续补充这些知识点.上章是使用了Microsof ...
- Servlet监听器笔记总结
监听器Listener的概念 监听器的概念很好理解,顾名思义,就是监视目标动作或状态的变化,目标一旦状态发生变化或者有动作,则立马做出反应. Servlet中的也有实现监听器的机制,就是Listene ...
- VS2015常用快捷键总结
生成解决方案 F6,生成项目Shift+F6 调试执行F5,终止调试执行Shift+F5 执行调试Ctrl+F5 查找下一个F3,查找上一个Shift+F3 附加到进程Ctrl+Alt+P,逐过程F1 ...
- 软件工程(C编码实践篇)学习心得
孟繁琛 + 原创作品转载请注明出处 + <软件工程(C编码实践篇)>MOOC课程 http://mooc.study.163.com/course/USTC-1000002006 软件工程 ...
- MongoDB学习笔记~对集合属性的操作
回到目录 $unset清除元素 请注意在单个数组元素上使用$unset的结果可能与你设想的不一样.其结果只是将元素的值设置为null,而非删除整个元素.要想彻底删除某个数组元素,可以用$pull 和$ ...