leetcood学习笔记-110-平衡二叉树
---恢复内容开始---
题目描述:


方法一:
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-平衡二叉树的更多相关文章
- Android(java)学习笔记110:Java中操作文件的类介绍(File + IO流)
1.File类:对硬盘上的文件和目录进行操作的类. File类是文件和目录路径名抽象表现形式 构造函数: 1) File(String pathname) Creat ...
- leetcood学习笔记-20
python字符串与列表的相互转换 学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.s ...
- leetcood学习笔记-14*-最长公共前缀
笔记: python if not 判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不 ...
- leetcood学习笔记-54-螺旋矩阵
题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0 ...
- Android(java)学习笔记110:ScrollView用法
理论部分 1.ScrollView和HorizontalScrollView是为控件或者布局添加滚动条 2.上述两个控件只能有一个孩子,但是它并不是传统意义上的容器 3.上述两个控件可以互相嵌套 4. ...
- daily english dictation 学习笔记[1-10]
b站网址https://www.bilibili.com/video/av17188299/?p=2 1. Mother Teresa, who received a Nobel Peace Priz ...
- leetcood学习笔记-35-二分法
题目: 第一次提交; class Solution: def searchInsert(self, nums: List[int], target: int) -> int: for i in ...
- leetcood学习笔记-28-KMP*
题目: 第一次提交: class Solution: def strStr(self, haystack: str, needle: str) -> int: if not len(needle ...
- leetcood学习笔记-27-移除元素
题目: 第一次提交: class Solution: def removeElement(self, nums, val: int) -> int: for i in range(len(num ...
随机推荐
- 记录我个人对Telegram的了解
对Telegram(电报) 开始的了解是以为提供了Telegram API,就可以基于它进行开发自己的即时通讯(Instant Messaging)程序. 大概使用过: webogram 和 tele ...
- hdu 3746 kmp的next数组理解
题目大意: 求最少在结尾补上几个字符才能形成循环 基本思路: next数组有一个性质,长度为len的字符串的最小长度的循环节(可能没有,但有的话一定是)len-next[len],因为最长不能是原串, ...
- thymeleaf onclick方法向js方法传递参数
如下图 这个错误并不影响 请放心使用
- Faster-RCNN论文精读
State-of-the-art object detection networks depend on region proposal algorithms to hypothesize objec ...
- js过滤字符串中的html标签
var str = 'add<a>daad</a><p>fsdada</p>' str.replace(/<[^<>]+>/g, ...
- Yii2中的规则
//Yii2中的规则,用户名的规则,1.用户名只能是字母 2.判断用户名已经存在 3.用户名的长度 4.用户名不能为空 [['username'], 'match', 'pattern' => ...
- Openstack 中的消息总线 & AMQP
目录 目录 消息总线 消息总线的原理 AMQP 消息总线 Openstack 采用了面向服务的开发模式(有别于面向对象和面向过程),需要我们去考虑各个服务之间和各项目之间是如何传递消息的. Restf ...
- mysql中explain详解
explain语法 有两种用法: 1.EXPLAIN tbl_name 2.EXPLAIN [EXTENDED] SELECT select_options 为了更好的说明它,我们需要建两张表, ...
- Django框架(七)—— 模板层:变量、过滤器、标签、自定义标签和过滤器
目录 模板层:变量.过滤器.标签.自定义标签和过滤器 一.模板层变量 1.语法 2.使用 二.模板层之过滤器 1.语法 2.常用过滤器 3.其他过滤器 三.模板值标签 1.for标签 2.if标签 3 ...
- laydate box-sizingCSS就会变形
*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;/* box-sizing:border-box; */} 解决:在/laydat ...