[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 ...
随机推荐
- M - Pots
You are given two pots, having the volume of A and B liters respectively. The following operations c ...
- 一名前端Web架构师的成长之路(转载)
本人也是coding很多年,虽然很失败,但也总算有点失败的心得,不过我在中国,大多数程序员都是像我一样,在一直走着弯路.如果想成为一个架构师,就必须走正确的路,否则离目标越来越远,正在辛苦工作的程序员 ...
- Python基础爬虫
搭建环境: win10,Python3.6,pycharm,未设虚拟环境 之前写的爬虫并没有架构的思想,且不具备面向对象的特征,现在写一个基础爬虫架构,爬取百度百科,首先介绍一下基础爬虫框架的五大模块 ...
- 2018ACM-ICPC焦作区域赛【反思总结】
摸银结束回来,整个人都轻松了. 自CCPC打铁以来的这两个月真的太痛苦了. 俱乐部退役的退役停训的停训,好冷清啊. 前期切题很稳,前四题两个小时1A. 过了四题之后好像心态有点飘,然后开题就慢了,想题 ...
- 跳石头|河中跳房子|NOIP2015提高组T4|二分法
喵 提交地址:http://codevs.cn/problem/4768/ 题目: 题意:自己看 思路: 1.读入各个石头数据 2.直接二分答案: 枚举一个石头i和一个石头j,要求i和j之间的距离为m ...
- 被Entity Framework Core的细节改进震撼了一下
今天用 SQL Server Profiler 查看 Entity Framework Core 生成的 SQL 语句时,突然发现一个细节改进,并且被它震撼了一下: exec sp_executesq ...
- ubuntu16.04下安装opencv3.4.1及其扩展模块
1.源文件下载 opencv-3.4.1.tar.gz(https://github.com/opencv/opencv/releases) opencv_contrib-3.4.1.tar.gz(h ...
- 线程同步-使用CountDownEvent类
CountDownEvent类:信号类,等待直到一定数量的操作完成. 代码Demo: using System; using System.Threading; Main方法下面加入以下代码片段: p ...
- 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1, ...
- vue中的provide/inject的学习
在 Vue.js 的 2.2.0+ 版本中添加加了 provide 和 inject 选项.用于父级组件向下传递数据.provide/inject:简单的来说就是在父组件(或者曾祖父组件)中通过pro ...