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 ...
随机推荐
- 使用redis构建可靠分布式锁
关于分布式锁的概念,具体实现方式,直接参阅下面两个帖子,这里就不多介绍了. 分布式锁的多种实现方式 分布式锁总结 对于分布式锁的几种实现方式的优劣,这里再列举下 1. 数据库实现方式 优点:易理解 缺 ...
- 用html5的canvas和JavaScript创建一个绘图程序
本文将引导你使用canvas和JavaScript创建一个简单的绘图程序. 创建canvas元素 首先准备容器Canvas元素,接下来所有的事情都会在JavaScript里面. <canvas ...
- Bootstrap 模态框(Modal)插件
页面效果: html+js: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 12、Struts2表单重复提交
什么是表单重复提交 表单的重复提交: 若刷新表单页面, 再提交表单不算重复提交. 在不刷新表单页面的前提下: 多次点击提交按钮 已经提交成功, 按 "回退" 之后, 再点击 &qu ...
- JavaScript基础知识总结(二)
JavaScript语法 二.数据类型 程序把这些量.值分为几大类,每一类分别叫什么名称,有什么特点,就叫数据类型. 1.字符串(string) 字符串由零个或多个字符构成,字符包括字母,数字,标点符 ...
- bash字符串操作
参考 http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html 问题:bash怎么提取字符串的最后一位?例如python中strin ...
- 邻接表的广度优先遍历(java版)
到 0 的权是 91 到 2 的权是 31 到 3 的权是 61 到 4 的权是 7 2 到 0 的权是 22 到 3 的权是 5 3 到 0 的权是 33 到 4 的权是 1 4 到 2 的权是 2 ...
- mono for android学习过程系列教程(1)
直接进入主题,关于mono for android的学习,首先配置好环境,如何配置环境,度娘谷歌一大堆,记得使用破解版. 我自己是百度“黑马四期”传智播客的视频,里面有破解版开发环境的软件. 今天直接 ...
- 基于Netty打造RPC服务器设计经验谈
自从在园子里,发表了两篇如何基于Netty构建RPC服务器的文章:谈谈如何使用Netty开发实现高性能的RPC服务器.Netty实现高性能RPC服务器优化篇之消息序列化 之后,收到了很多同行.园友们热 ...
- Lesson 18 He often does this!
Text After I had had lunch at a village pub, I looked for my bag. I had left it on a chair beside th ...