Leetcode 110. Balanced Binary Tree
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.
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
factor = abs(self.level(root.left) - self.level(root.right))
return factor < 2 and self.isBalanced(root.right) and self.isBalanced(root.left) def level(self, root):
if not root:
return 0
return max(self.level(root.left), self.level(root.right)) + 1
这里的方法使用了递归,每个subtree会多次计算level。虽然可以通过OJ, 但是可以使用DP提高效率。 建立一个Hash table, root作为key, level函数的值作为value。
d = {}
class Solution(object):
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
factor = abs(self.level(root.left) - self.level(root.right))
return factor < 2 and self.isBalanced(root.left) and self.isBalanced(root.right)
def level(self, root):
if not root:
return 0
if root in d:
return d[root]
else:
d[root] = max(self.level(root.left), self.level(root.right)) + 1
return d[root]
Leetcode 110. Balanced Binary Tree的更多相关文章
- 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] 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 ...
- leetcode 110 Balanced Binary Tree ----- java
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Java [Leetcode 110]Balanced Binary Tree
题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...
- [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. 原文 Given a bina ...
随机推荐
- Java设计模式之-----策略模式
首先,我们来看下策略模式的概念.一般的解释如下: 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化.(原文:The St ...
- 清北学堂2017NOIP冬令营入学测试 P4744 A’s problem(a)
清北学堂2017NOIP冬令营入学测试 P4744 A's problem(a) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试题,每三天结算 ...
- python install
Install all dependencies by pip install -r requirements.txt (Run this command from project root)
- Java 基础【11】@注解
1.注解简介 JDK 1.5 中引入的 java.lang.annotation 包提供注解编程支持,可以让类在编译.类加载.运行时被读取,并执行相应的处理. 在 Java EE应用的时候,总是免不了 ...
- Vuforia AR SDK入门
Vuforia是一个能让应用拥有视觉的软件平台.开发者借助它可以很轻松地为任何应用添加先进计算机视觉功能,允许你识别图片和物体,或者在真实世界中重建环境内容. 如果你现在正在制作一些可交互的市场活动项 ...
- 开源 XFControls , 用于 Xamarin.Forms 的自定义控件集
从此以后不会在博客园上发表任何言论,观注我的同志们,洗洗睡吧. ---------------------- 博文移至: http://www.jianshu.com/p/3ed1a3f10955
- Tensorflow学习笔记1:Get Started
关于Tensorflow的基本介绍 Tensorflow是一个基于图的计算系统,其主要应用于机器学习. 从Tensorflow名字的字面意思可以拆分成两部分来理解:Tensor+flow. Tenso ...
- Bootstrap系列 -- 6. 列表
一. 普通无序列表 <ul> <li>无序列表项目</li> <li>无序列表项目</li> <li>无序列表项目</li ...
- [BZOJ3144][HNOI2013]切糕(最小割)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3144 分析:神题不解释 http://www.cnblogs.com/zig-zag/ ...
- jQuery问题集锦
[1]阻止提交表单 方法1: $(function () { $("input[type=submit]").click(function (event) { //如果不满足表单提 ...