#-*- coding: UTF-8 -*-
#平衡二叉树
# 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):
    isbalanced=True  
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root==None:return True
        if root.left==None and root.right==None:return True
        self.dfs(root)
        return self.isbalanced
    
    def dfsDepth(self,root):
        if root==None:return 0
        leftDepth=self.dfsDepth(root.left)
        rightDepth=self.dfsDepth(root.right)
        
        return leftDepth+1 if leftDepth >rightDepth else (rightDepth+1)

    def dfs(self,root):
        if root==None:return
        leftDepth=self.dfsDepth(root.left)
        rightDepth=self.dfsDepth(root.right)
        if abs(leftDepth-rightDepth)>1:
            self.isbalanced=False
        else:
            self.dfs(root.left)
            self.dfs(root.right)

【leetcode❤python】110. Balanced Binary Tree的更多相关文章

  1. 【leetcode❤python】226. Invert Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  2. 【一天一道LeetCode】#110. Balanced Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】110. Balanced Binary Tree

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

  4. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

  5. 【leetcode❤python】 67. Add Binary

    class Solution(object):    def addBinary(self, a, b):        """        :type a: str  ...

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

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

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

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

  8. 110. Balanced Binary Tree - LeetCode

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

  9. Leetcode 笔记 110 - Balanced Binary Tree

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

随机推荐

  1. PTPX中的time_based analysis

    根据VCD文件的type,PTPX支持instantaneous peak power analysis和cycle_accurate peak power analysis. Time-Based ...

  2. [py]安装ipython

    系统:crunch bang11+python2.7.3 准备工具: sudo apt-get install python-pip python-dev build-essential 安装setu ...

  3. 修改linux下mysql目录权限

    1.进入当前目录:例cd /srun3/mysql 2.修改权用户限 chown -R mysql ipt_authd_remote(表示给ipt_authd_remotemysql权限) 3.修改用 ...

  4. NOIP200205均分纸牌

                                                                  均分纸牌 描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张 ...

  5. AMD机制与cMD的区别和概念简要介绍

    1.http://www.cnblogs.com/dojo-lzz/p/4707725.html 2.http://blog.chinaunix.net/uid-26672038-id-4112229 ...

  6. MySQL OnlineDDL

    参考资料: http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html http://www.mysqlperfo ...

  7. Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值

    Thinkphp用exp表达式执行mysql语句,查询某字段不为空is not null,自动增值 Thinkphp 的文档经常不够完整的表达MYSQL的各种组合,is not null在thinkp ...

  8. Linux Kernel之flush_cache_all在ARM平台下是如何实现的【转】

    转自:http://blog.csdn.net/u011461299/article/details/10199989 版权声明:本文为博主原创文章,未经博主允许不得转载. 在驱动程序的设计中,我们可 ...

  9. MySQL存储引擎之InnoDB

    一.The InnoDB Engine Each InnoDB table is represented on disk by an .frm format file in the database ...

  10. Codeforces 746D:Green and Black Tea(乱搞)

    http://codeforces.com/contest/746/problem/D 题意:有n杯茶,a杯绿茶,b杯红茶,问怎么摆放才可以让不超过k杯茶连续摆放,如果不能就输出NO. 思路:首先,设 ...