#-*- 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. 夺命雷公狗---node.js---10之POST的接收

    首先我们在项目下创建一个表单,代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  2. test generation和MBIST

    The problem of test generation Random test generation Deterministic algorithm for test generation fo ...

  3. Silverlight ModelView中调用UI进程

    Silverlihgt:  Deployment.Current.Dispatcher.BeginInvoke wpf: App.Current.Dispatcher.Invoke  

  4. php文件上传参数设置

    php默认的 上传文件大小是2M,要上传超过此大小的文件,需要设置php和apache的一些参数,具体参考如下: 1.file_uploads:是否允许通过HTTP上传文件的开关,默认为ON就是开 2 ...

  5. scala模式匹配

    package com.ming.test /** * 模式匹配 */ object MatchTest { def main(args: Array[String]): Unit = { //mat ...

  6. 将edit ctrL弄的像个dos

    case WM_CTLCOLOREDIT: { HWND hShellText = GetDlgItem(hDlg,IDC_TXT_SHELL); if (hShellText == (HWND)lP ...

  7. oracle 自定义聚合函数(MAX_O3_8HOUR_ND) 计算最大的臭氧8小时滑动平均值

    create or replace function MAX_O3_8HOUR_ND(value NUMBER) return NUMBER parallel_enable aggregate usi ...

  8. ExtJS的MessageBox总结

    自己写了个ExtJS的MsgBox的小模版,以后遇到需要使用提示的地方就拿过来改改,免得每次都重新写. /**MsgBox start**/ Ext.Msg.buttonText.yes = &quo ...

  9. ORACLE添加表约束的语法示例

    转自:http://jingyan.baidu.com/article/f54ae2fccda68d1e93b84942.html 示例: --班级表 CREATE TABLE TCLASS( cl_ ...

  10. MyISAM表杂记实验

    一.本文说明 由于刚学mysql所以动手做了一些实验. 二.实验内容 1.验证MyISAM有AUOT_INCREMENT coloumn功能 ----在这里是对现有表t,增加一个主键----mysql ...