---恢复内容开始---

题目描述:

方法一:

class Solution(object):
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
if abs(self.countfloor(root.left)-self.countfloor(root.right))>:
return False
else:
return self.isBalanced(root.left) and self.isBalanced(root.right) def countfloor(self,root):
if not root:
return
else:
return max(self.countfloor(root.left),self.countfloor(root.right))+

法二;效率高

class Solution(object):

    is_stop = False     # 为了加快计算速度, 标志是否已经有结果了

    def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
""" def get_depth_and_balance(root):
if self.is_stop: # 已经是非平衡二叉树, 就不需要再计算
return , False if not root:
return , True
if not root.left and not root.right:
return , True ldep, l_is_balanced = get_depth_and_balance(root.left)
rdep, r_is_balanced = get_depth_and_balance(root.right) depth = max(ldep, rdep) +
is_balanced = abs(ldep- rdep) <= and l_is_balanced and r_is_balanced if not is_balanced:
self.is_stop = True
return depth, is_balanced depth, is_balanced = get_depth_and_balance(root)
return is_balanced

leetcood学习笔记-110-平衡二叉树的更多相关文章

  1. Android(java)学习笔记110:Java中操作文件的类介绍(File + IO流)

    1.File类:对硬盘上的文件和目录进行操作的类.    File类是文件和目录路径名抽象表现形式  构造函数:        1) File(String pathname)       Creat ...

  2. leetcood学习笔记-20

    python字符串与列表的相互转换   学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.s ...

  3. leetcood学习笔记-14*-最长公共前缀

    笔记: python if not   判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不 ...

  4. leetcood学习笔记-54-螺旋矩阵

    题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0 ...

  5. Android(java)学习笔记110:ScrollView用法

    理论部分 1.ScrollView和HorizontalScrollView是为控件或者布局添加滚动条 2.上述两个控件只能有一个孩子,但是它并不是传统意义上的容器 3.上述两个控件可以互相嵌套 4. ...

  6. daily english dictation 学习笔记[1-10]

    b站网址https://www.bilibili.com/video/av17188299/?p=2 1. Mother Teresa, who received a Nobel Peace Priz ...

  7. leetcood学习笔记-35-二分法

    题目: 第一次提交; class Solution: def searchInsert(self, nums: List[int], target: int) -> int: for i in ...

  8. leetcood学习笔记-28-KMP*

    题目: 第一次提交: class Solution: def strStr(self, haystack: str, needle: str) -> int: if not len(needle ...

  9. leetcood学习笔记-27-移除元素

    题目: 第一次提交: class Solution: def removeElement(self, nums, val: int) -> int: for i in range(len(num ...

随机推荐

  1. 第五章 配置私有仓库Harbor

    一.Harbor 安装(尚硅谷资料) 安装:Harbor 官方地址:官方地址:https://github.com/vmware/harbor/releases 1.解压软件包 tar xvf har ...

  2. 关于prototype与constructor的几点理解

    首先要明确一点,js中的prototype是每个函数创建时(并非执行时,而是函数存在于堆区后)自动创建一个默认对象,这一对象中可以动态的添加属性 在new对象后,可以通过“对象.属性名”的方式直接调用 ...

  3. C stdarg.h

    参考:https://www.cnblogs.com/bettercoder/p/3488299.html    博主:运动和行动 va_start宏,获取可变参数列表的第一个参数的地址(list是类 ...

  4. PHP-全排列

    给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] ...

  5. css负边距之详解(子绝父相)

    来源 | http://segmentfault.com 原文 |  The Definitive Guide to Using Negative Margins   自从1998年CSS2作为推荐以 ...

  6. webpack起的项目怎么用手机访问?

    默认情况下 webpack-dev-server只能通过 localhost 访问 如果需要通过 ip 地址访问,修改 npm 配置文件中的 scripts 属性中对应的启动脚本 例如 start 或 ...

  7. appium1.4+华为8.0执行自动化脚本,报启动session失败,原因是unicode_ime_apk\Uni codeIME-debug.apk在手机上已存在,再次安装失败,导致启动session失败,解决办法:换高版本的appium

    最开始做Android自动化测试时,通过执行脚本发现报,已安装UnicodeIME-debug.apk,再次安装失败,当时觉得这个apk对我来说没用,就把D:\Program Files (x86)\ ...

  8. Java中的List集合

    List集合继承自collection接口,他自己也是个接口,没有具体的结构,与Set集合不同,List集合允许重复的元素. List集合特有方法:(Collection中没有这些) 这些在Arral ...

  9. IO流分类

    IO流在java中从输入和输出角度分类: 1.输入流 2.输出流 IO流在java中从数据的角度来分类: 1.字符流:文本,我们能读懂的都可以认为是字符流,如:文章,java文件等 字符输入流的超类: ...

  10. Android中怎么破解游戏之修改金币数

    我们在玩游戏的时候总是会遇到一些东东需要进行购买的,但是我们可能又舍不得花钱,那么我们该怎么办呢?那就是用游戏外挂吧!我们这里说的是Android中的游戏,在网上搜索一下移动端游戏外挂,可能会找到一款 ...