当前位置:网站首页>【leetcode】107.二叉树的层序遍历II
【leetcode】107.二叉树的层序遍历II
2022-04-23 10:27:00 【前端corner】
题目
给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[15,7],[9,20],[3]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
提示:
树中节点数目在范围 [0, 2000] 内
-1000 <= Node.val <= 1000
思路

- 想想这道题目和这一道【leetcode】102.二叉树的层序遍历题目遍历结果会有什么区别呢?其实就是那道题目结果数组翻转一下就行了。
- 翻转的操作可以在往结果数组里添加单层遍历结果时进行,即从数组头部添加。
代码

/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */
/** * @param {TreeNode} root * @return {number[][]} */
var levelOrderBottom = function(root) {
if(!root) return []
let queue = [root]
let res = []
while(queue.length){
const len = queue.length
let curLevel = [] //存放每一层的节点
for(let i = 0 ; i < len ; i++){
const curNode = queue.shift()
curLevel.push(curNode.val)
if(curNode.left) queue.push(curNode.left)
if(curNode.right) queue.push(curNode.right)
}
res.unshift(curLevel) //存放当层遍历结果,从数组头部添加
}
return res
};
复杂度
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( n ) O(n) O(n)
关注我的专栏,每天更新三道leetcode题解,一起变强!
版权声明
本文为[前端corner]所创,转载请带上原文链接,感谢
https://blog.csdn.net/laplacepoisson/article/details/124359081
边栏推荐
- Solution architect's small bag - 5 types of architecture diagrams
- MapReduce core and foundation demo
- Sim Api User Guide(7)
- Can Jerry's AES 256bit [chapter]
- MapReduce compression
- Detailed explanation of MapReduce calculation process
- 一文看懂 LSTM(Long Short-Term Memory)
- Shell script interaction free
- Contact between domain name and IP address
- Realizing data value through streaming data integration (5) - flow analysis
猜你喜欢

Yarn resource scheduler

net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。

JVM——》常用命令

Example of pop-up task progress bar function based on pyqt5

Sim Api User Guide(6)

JVM——》常用参数

Windows installs redis and sets the redis service to start automatically

Juc并发编程09——Condition实现源码分析

mysql同一个表中相同数据怎么合并

101. Symmetric Tree
随机推荐
第120章 SQL函数 ROUND
Realizing data value through streaming data integration (4) - streaming data pipeline
Ansible playbook syntax and format automate cloud computing
19、删除链表的倒数第N个节点(链表)
ansible 云计算 自动化
209、长度最小的子数组(数组)
0704、ansible----01
Construction and traversal of binary tree
209. Subarray with the smallest length (array)
利用多线程按顺序连续输出abc10次
MySql常用语句
shell脚本免交互
任意文件读取漏洞 利用指南
Jerry's more accurate determination of abnormal address [chapter]
Understand the new economic model of platofarm and its ecological progress
通过流式数据集成实现数据价值(5)- 流处理
ansible 云计算 自动化 命令行精简版
DBA常用SQL语句(2)— SGA和PGA
/Can etc / shadow be cracked?
454、四数之和(哈希表)
