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 ...
随机推荐
- Redis 管道(pipeline)
- nodejs 模板引擎jade的简单使用
1.jade html head style script body div ul li li jade1.js var jade=require('jade'); var str=jade.rend ...
- vue组件绑定原生事件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Mysql学习笔记(001)-常见命令
常见命令 SHOW DATABASES; /* use test; create database myemployees; use myemployees; create table employe ...
- 安全检测及分析神器—AppScan使用教程
最近项目准备验收,所以最近在做项目验收的准备工作:我们公司规定,项目的安全检测必须通过才能进行项目验收:公司的安全部门用的检测软件就是大名鼎鼎的IBM Rational Appscan;在教由安全部门 ...
- 组合数学(math)
组合数学(math) 题目描述 为了提高智商,zjy开始学习组合数学.某一天她解决了这样一个问题:“给一个网格图,其中某些格子有财宝.每次从左上角出发,只能往右或下走.问至少要走几次才能把财宝全部捡完 ...
- js委托事件-addEventListeners(冒泡方向)
JQuery中live().delegate().on()事件都能给新增元素绑定事件,原理就是用了事件委托. 实例: 给id为div的元素绑定一个click委托,如果冒泡上来的元素是P元素就会执行al ...
- ElasticSearch Roaring bitmap 和跳表联合查询
ElasticSearch Roaring map 先把所有数按65535划分, 划分方法就是求商和余数,商代表数字最终在哪一块,余数代表最终在块内的数字 比如 1, 65536, 65537, 13 ...
- 多图上传控制器及模型代码(2)thinkphp5+layui实现多图上传保存到数据库,可以实现图片自由排序,自由删除。
公共css代码 <style> .layui-upload-img { width: 90px; height: 90px; margin: 0; } .pic-more { width: ...
- PAT_A1084#Broken Keyboard
Source: PAT A1084 Broken Keyboard (20 分) Description: On a broken keyboard, some of the keys are wor ...