当前位置:网站首页>Leetcode107-二叉树的层序遍历II详解
Leetcode107-二叉树的层序遍历II详解
2022-07-25 23:55:00 【白羊by】
往期博客:
目录
题目
给你二叉树的根节点
root,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
示例

输入:root = [3,9,20,null,null,15,7] 输出:[[15,7],[9,20],[3]]
解析
广度优先算法
从上到下遍历每一层,将每一层的节点数子放入一个子集中,将每一层的子集按自底向上防毒结果集中。
初始化变量size、result和queue,其中size表示每一层节点的个数,result用来存放每一层的子集,queue为队列。
对于示例,首先从根节点3开始,第一步将节点3加入队列中,此时size的大小为队列的长度1,第二步取出队列中的节点3放入到结果集中,并将size-1

第三步将左右孩子节点放入队列中,此时size的大小为队列长度2,第四步取出队列的第一个元素放入结果集中,并将size-1

第五步取出队列中的元素20放入结果集中,并将size-1,第六步将节点20的左右孩子节点放入队列中,此时size大小为队列长度2

第七步将队列的第一个元素放入结果集中,将size-1,第八步将队列最后一个元素放入结果集中,并将size-1

深度优先算法
本题的深度优先算法流程和Leetcode102-二叉树的层序遍历是一样的,只不过将最后的result翻转一下,将自上向下遍历变为自下向上遍历

代码
广度优先搜索
from collections import deque
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
result = []
if root is None:
return result
q = deque([])
q.append(root)
temp = deque([])
while len(q) > 0:
size = len(q)
ls = []
while size > 0:
cur = q.popleft()
ls.append(cur.val)
if cur.left is not None:
q.append(cur.left)
if cur.right is not None:
q.append(cur.right)
size = size -1
temp.appendleft(ls[:])
result = list(temp)
return result深度优先搜索
from collections import deque
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
result = []
if root is None:
return result
self.dfs(root, result, 0)
result.reverse() # 将结果翻转
return result
def dfs(self, node, result, level):
if node is None:
return
if level > len(result)-1:
result.append([])
result[level] .append(node.val)
if node.left is not None:
self.dfs(node.left, result, level+1)
if node.right is not None:
self.dfs(node.right, result, level+1)边栏推荐
- 二叉树——110. 平衡二叉树
- MySQL的DDL、DML和DQL的基本语法
- The process of finding free screen recording software - I didn't expect win10 to come with this function
- VSCode格式化Json文件
- 行为型模式之观察者模式
- Yolov3 trains its own data set
- String functions and memory operation functions
- 安全文档归档软件
- TOPSIS and entropy weight method
- Redis extended data type (jump table /bitmaps/hyperloglog/geospatial)
猜你喜欢

二叉树——257. 二叉树的所有路径

Swap, move, forward, exchange of utility component learning

Fixed and alternate sequential execution of modes

LeetCode 0135. 分发糖果

Shardingsphere data slicing

下一代终端安全管理的关键特征与应用趋势

A long detailed explanation of C language operators

Leetcode 0919. complete binary tree inserter: array representation of complete binary tree

二叉树——111. 二叉树的最小深度

Generating random number random learning uniform_ int_ distribution,uniform_ real_ distribution
随机推荐
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
调用钉钉api报错:机器人发送签名过期;solution:签名生成时间和发送时间请保持在 timestampms 以内
二叉树——111. 二叉树的最小深度
Lua script Wireshark plug-in to resolve third-party private protocols
Song of statistics lyrics
Good news under the epidemic
The GUI interface of yolov3 (simple, image detection)
Data intensive application system design - Application System Overview
Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data
Zhiniu stock -- 09
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
[learning notes] solid works operation record
C# - readonly 和 const 关键字
什么叫做 inode ?带你理解 inode 和对于创建文件和删除文件时 inode 都提供了哪些帮助。
红娘的话
[Muduo] thread package
@The underlying principle of Autowired annotation
Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms
Write a select drop-down list
二叉树——617. 合并二叉树