[LeetCode] 110. Balanced Binary Tree_Easy tag: DFS
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.
Example 1:
Given the following tree [3,9,20,null,null,15,7]
:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]
:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
最简单的思路就是建一个height function, 可以计算每个node的height, 然后abs(left_height - right_height) <2, 再recursive 判断即可.
04/20/2019 Update : add one more solution using Divide and conquer. (add a class called returnType)
1. Constraints
1) None => True
2. Ideas
DFS T: O(n^2) optimal O(n) S; O(n)
3. Code
1) T: O(n^2)
class Solution:
def isBalance(self, root):
if not root: return True
def height(root):
if not root: return 0
return 1 + max(height(root.left) , height(root.right))
return abs(height(root.left) - height(root.right)) < 2 and self.isBalance(root.left) and self.isBalance(root.right)
2) T: O(n) S; O(n)
bottom to up, 一旦发现不符合的,就不遍历, 直接返回-1一直到root, 所以不需要每次来计算node的height.
class Solution:
def isBalance(self, root):
def height(root):
if not root: return 0
l, r = height(root.left), height(root.right)
if -1 in [l, r] or abs(l-r) >1: return -1
return 1 + max(l,r)
return height(root) != -1
3) T: O(n) . S: O(n)
class ResultType:
def __init__(self, isBalanced, height):
self.isBalanced = isBalanced
self.height = height class Solution:
def isBalanced(self, root: TreeNode) -> bool:
def helper(root):
if not root:
return returnType(True, 0)
left = helper(root.left)
right = helper(root.right)
if not left.isBalanced or not right.isBalanced or abs(left.height - right.height) > 1:
return ResultType(False, 0)
return ResultType(True, 1 + max(left.height, right.height))
return helper(root).isBalanced
[LeetCode] 110. Balanced Binary Tree_Easy tag: DFS的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- [LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- leetcode 110 Balanced Binary Tree(DFS)
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 binary ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 110. Balanced Binary Tree (Tree; DFS)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 看看大神们是怎么解决一些【bng】的哪!!!!
作者:姚冬 遇到bng的分享 我曾经做了两年大型软件的维护工作,那个项目有10多年了,大约3000万行以上的代码,参与过开发的有数千人,代码checkout出来有大约5个GB,而且bug特别多,op ...
- JQ-总结
-----------------------------------------------------------------------------jQuery----------------- ...
- hdparm命令(转)
转自:http://man.linuxde.net/hdparm hdparm命令提供了一个命令行的接口用于读取和设置IDE或SCSI硬盘参数. 语法 hdparm(选项)(参数) 选项 -a< ...
- 洛谷P1118 数字三角形【dfs】【STL】
题目链接:https://www.luogu.org/problemnew/show/P1118 题意: 1~n的一个排列,相邻的两项加起来得到下一行. 现在给定最后一行的数字,问最初的1~n的排列是 ...
- JSCS: Please specify path to 'JSCS' package
JSCS: Please specify path to 'JSCS' package 1.webStrom提示: JSCS: Please specify path to 'JSCS' packag ...
- app相关的一些网站
https://www.qimai.cn 排名排行关键字原aso100
- 如果是多个 c 代码的源码文件,编译方法如下: $ gcc test1.c test2.c -o main.out $ ./main.out test1.c 与 test2.c 是两个源代码文件。
如果是多个 c 代码的源码文件,编译方法如下: $ gcc test1.c test2.c -o main.out $ ./main.out test1.c 与 test2.c 是两个源代码文件.
- 2012年蓝桥杯省赛A组c++第1题(xy迭代增殖)
/* 微生物增殖 题目: 假设有两种微生物 X 和 Y X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍). 一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分 ...
- Android+Tomcat通过http获取本机服务器资源
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- Copycat - 状态
Member.Status status的变迁是源于heartbeat heartbeat,append空的entries /** * Triggers a heartbeat to a majori ...