#-*- 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. JSP-03-实现数据传递

    会话跟踪:隐藏表单域.URL重写和Cookie 3.1 传参方法 get /post 区别   3.2  request 获取参数 数据类型  变量名  =  (数据类型)request.getPar ...

  2. QTP常用功能

    1.QTP录制过程的截图 查看录制脚本过程中QTP的截图可以在QTP中查找,在关键字视图中点击每一步都对应一个截图   2.在关键字视图中为测试步骤添加注释 在关键字视图中表格列头中单击鼠标右键,选择 ...

  3. 匹配 prev 元素之后的所有 siblings 元素

    描述: 找到所有与表单同辈的 input 元素 HTML 代码: <form> <label>Name:</label> <input name=" ...

  4. 【python cookbook】【字符串与文本】16.以固定的列数重新格式化文本

    问题:重新格式化一些很长的字符串,以指定的列数来显示 解决方案:textwrap模块的fill()方法来实现 # A long string s = "Look into my eyes, ...

  5. iOS 学习笔记 十三 (2015.04.15)采用第三方库,实现ios录音转为amr

    1.第三方开源库地址 https://github.com/guange2015/ios-amr 2.参考博客地址 http://blog.csdn.net/windsoul85/article/de ...

  6. 那些情况该使用它们spin_lock到spin_lock_irqsave【转】

    转自:http://blog.csdn.net/wesleyluo/article/details/8807919 权声明:本文为博主原创文章,未经博主允许不得转载. Spinlock的目的是用来同步 ...

  7. run loop 输入源

    做了一年多的IOS开发,对IOS和Objective-C深层次的了解还十分有限,大多还停留在会用API的级别,这是件挺可悲的事情.想学好一门语言还是需要深层次的了解它,这样才能在使用的时候得心应手,出 ...

  8. HDU 5795:A Simple Nim(博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description   Two players take t ...

  9. Windows通过DOS命令进入MYSQL的方法

    例:MYSQL安装在 D:\ApacheServer\mysql 下 开始==>运行==>cmd,或者 按住win键+r键输入cmd C:\Users\Administrator>d ...

  10. android 数据库操作

    做android的数据库方面用的很少,所以用的时候记录下来,下次碰到直接copy,一下代码是最基本的实现: 首先是继承helper类: package com.create.rycreateim.db ...