#-*- 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. Hibernate开始上手总结

    1,导入hibernate 的jar包,c3p0jar包 2,创建和表关联的实体类,创建关联实体类的配置文件 package com.entity; public class News { priva ...

  2. 导出excel部分代码

    public static function header_file($doc,$file,$title,$type='Excel5'){ if(!empty($doc)){ $objWriter = ...

  3. 衣明志是个SB

    面试碰到衣明志,问了些傻逼问题,尼玛就是一不折不扣的蠢驴. 这个人太能装了,而且水平也不咋地.

  4. js编写规范

    JavaScript编码规范 Bug----33条 1. 不要使用’==’和’!=’,使用’===’和’!==’替代 等级:Major 原因:==和!=在判断值相等前会判断类型是否相等.这容易因为类型 ...

  5. html规范整体结构

    <!DOCTYPE html><html lang="zh"><head> <meta charset="utf-8" ...

  6. xUtils之ViewUtil

    要使用xutils,首先要导入xutils类库. 其次要添加权限: <uses-permission android:name="android.permission.WRITE_EX ...

  7. COM编程之一 组件

    [1]组件产生的背景 一个应用程序通常是由单个二进制文件组成的. 当应用程序版本发布后一般不会发生任何变化,对于操作系统.硬件以及客户需求的改变都必须要等到修复源代码后且整个应用程序被重新编译才可处理 ...

  8. Delphi xe 下快捷使用 FastMM 的内存泄露检测功能

    Delphi xe 集成了FastMM,调试程序是的时候可以方便地检查内存泄露了.  使用方法:在project中,添加一行: ReportMemoryLeaksOnShutdown := Debug ...

  9. python读入文件

    举例说明吧,路径一定要带转义符‘\’,下面的例子中,每一行是一个样本的feature >>> myfile=open("D:\\301\\graduate_thesis\\ ...

  10. 【Pro ASP.NET MVC 3 Framework】.学习笔记.5.SportsStore一个真实的程序

    我们要建造的程序不是一个浅显的例子.我们要创建一个坚固的,现实的程序,坚持使它成为最佳实践.与Web Form中拖控件不同.一开始投入MVC程序付出利息,它给我们可维护的,可扩展的,有单元测试卓越支持 ...