LeetCode:110(Python)—— 平衡二叉树(简单)
平衡二叉树
概述:给定一个二叉树,判断它是否是高度平衡的二叉树。一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
输入:root = [3,9,20,null,null,15,7]
输出:true输入:root = [1,2,2,3,3,null,null,4,4]
输出:false输入:root = []
输出:true
方法一:自顶向下的递归
思路:定义函数 height,用于计算二叉树中的任意一个节点 p 的高度。对于当前遍历到的节点,首先计算左右子树的高度,如果左右子树的高度差是否不超过 1 ,再分别递归地遍历左右子节点,并判断左子树和右子树是否平衡。
# 自顶向下的递归
class Solution:def isBalanced(self, root: Optional[TreeNode]) -> bool:def height(root: TreeNode) -> int:if not root:return 0return max(height(root.left), height(root.right)) + 1if not root:return Truereturn abs(height(root.left) - height(root.right)) <= 1 \and self.isBalanced(root.left) and self.isBalanced(root.right)
方法二:自底向上的递归
思路:自底向上递归的做法类似于后序遍历,对于当前遍历到的节点,先递归地判断其左右子树是否平衡,再判断以当前节点为根的子树是否平衡。
# 自底向上的递归
class Solution:def isBalanced(self, root: Optional[TreeNode]) -> bool:def height(root: TreeNode) -> int:if not root:return 0left_height = height(root.left)right_height = height(root.right)if left_height == -1 or right_height == -1 \or abs(left_height - right_height) > 1:return -1else:return max(left_height, right_height) + 1return height(root) >= 0
总结
自底向上的递归就是及时止损,自顶向下的递归就是不管三七二十一先把所有的高度算一遍!
LeetCode:110(Python)—— 平衡二叉树(简单)相关推荐
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- LeetCode 排序和搜索简单部分 Python实现
#2018-06-07 June Thursday the 23 week, the 158 day SZ #LeetCode 排序和搜索 简单部分 Python实现 '''并两个有序数组 注意题目要 ...
- python爬虫简单实例-Python 利用Python编写简单网络爬虫实例3
利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站"http://bbs.51tes ...
- python打开是什么颜色-python实现简单颜色识别程序
本文实例为大家分享了python实现简单颜色识别程序的具体代码,供大家参考,具体内容如下 import numpy as np import cv2 font= cv2.FONT_HERSHEY_SI ...
- python画简单图-python绘制简单彩虹图
本文实例为大家分享了python绘制彩虹图的具体代码,供大家参考,具体内容如下 代码: from turtle import * #控制彩虹路径 def path(pen, r, g, b): pen ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
- python简单成绩录入,python实现简单成绩录入系统
学了一个多月的python,做了一个小程序:python实现简单成绩录入系统,实验一下 menu部分 from tkinter import*#这是一个python模块,python3中都有 impo ...
- LeetCode 110 Balanced Binary Tree
LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...
- 003.[python学习] 简单抓取豆瓣网电影信息程序
003.[python学习] 简单抓取豆瓣网电影信息程序 声明:本程序仅用于学习爬网页数据,不可用于其它用途. 本程序仍有很多不足之处,请读者不吝赐教. 依赖:本程序依赖BeautifulSoup4和 ...
- leetcode 中的链表简单题 python3
目录: 基本使用 // 21题 合并有序链表 // 83题 删除有序链表重复元素 // 141题 环形链表 // 160题 相交链表 // 203题 移除链表元素// 206题 反转链表 // 基本使 ...
最新文章
- 均匀分布取某一点概率_概率和概率分布
- 【ACM】CODE[VS] 1215 (DFS)
- Scalaz(7)- typeclass:Applicative-idomatic function application
- 【LeetCode】字符串 string(共112题)
- Java常见的乱码解决方式
- shiro缓存管理时报错java.lang.ClassCastException: com.xxx.User cannot be cast to com.xxx.User
- 重装谷歌浏览器chrome:未知错误导致安装失败
- Genome2D官方博客及教程
- spring mvc---controller返回值
- Spring源码全解
- stable diffusion制作韩国版AI女友关键词
- 黑马全新推出《2022年最实用的PS全套教程》
- python 蒙特卡罗_python实现蒙特卡罗方法教程
- Redhat最小化安装后再安装图形界面
- Luogu P1497题解
- win11更新后没有声音?来这里一站解决
- Springboot+vue项目音乐网站与分享平台
- 软考 - 07 机器学习应用开发平台
- 责任与任务--感谢航空线福亮和福生的分享
- 三代基因组全基因组的变异检测