题目来源


https://leetcode.com/problems/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.


题意分析


Input: a binary tree

Output: True or False

Conditions: 判断一颗树是不是平衡树,平衡树的意思是:对于每个节点而言,左右子树的最大深度不超过1


题目思路


递归判断。


AC代码(Python)

 # Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def Height(self, root):
if root == None:
return 0
return max(self.Height(root.left), self.Height(root.right)) + 1
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root == None:
return True
if abs(self.Height(root.left) - self.Height(root.right)) <= 1:
return self.isBalanced(root.left) and self.isBalanced(root.right)
else:
return False

[LeetCode]题解(python):110 Balanced Binary Tree的更多相关文章

  1. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  2. 110.Balanced Binary Tree Leetcode解题笔记

    110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  3. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  4. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  5. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  6. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  7. 【leetcode❤python】110. Balanced Binary Tree

    #-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):#     def _ ...

  8. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. LeetCode 110. Balanced Binary Tree (平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

随机推荐

  1. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  2. Ubuntu 12.04安装Google Chrome

    详细请参照:http://hi.baidu.com/kevin276/item/29bc1c96a208fabc82d29542 至于安装之后怎么打开,在终端输入google-chrome即可打开并会 ...

  3. DELPHI的split函数的各种实现方法(转)

    一.单字符 function split(s,s1:string):TStringList;beginResult:=TStringList.Create;while Pos(s1,s)>0 d ...

  4. 省份+城市---Dropdownlist控件的应用

    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> ...

  5. grep 命令搜索 带空格的字符

    grep - n ' a[[:space:]]b' 就能搜索到 'a    b'类似的字符了 如果要搜索带单引号的 用双引号括起来 如果要搜索带双引号的 用单引号括起来

  6. for循环的时候是按照数字递增会造成一些元素被遗漏

    $test_array=array(1,2,3,4,4,5,5,6); $test_array_unique=array_unique($test_array); print_r($test_arra ...

  7. 【液晶模块系列基础视频】3.4fatfs接口函数的使用4

    ============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...

  8. CSS2系列:BFC(块级格式化上下文)IFC(行级格式化上下文)

    BFC 块级格式化上下文,不好理解,我们暂且把她理解成"具有特殊的一类元素" 哪些元素会生成BFC? 根元素 float属性不为none position为absolute或fix ...

  9. PHP 多个文件上传

    关键函数: is_uploaded_file():用于判断指定的文件是否是通过 HTTP POST 上传的,如果是则返回 TRUE.用于防止潜在的攻击者对原本不能通过脚本交互的文件进行非法管理,这可以 ...

  10. PHP抓取采集类snoopy介绍

    PHP抓取采集类snoopy介绍 一个PHP的抓取方案 在 2011年07月04日 那天写的     已经有 10270 次阅读了 感谢 参考或原文   服务器君一共花费了14.288 ms进行了2次 ...