当前位置:网站首页>【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
边栏推荐
- Redis design and Implementation
- What are Jerry's usual program exceptions? [chapter]
- 利用多线程按顺序连续输出abc10次
- Operation of 2022 tea artist (primary) test question simulation test platform
- Comparison and practice of prototype design of knowledge service app
- Construction and traversal of binary tree
- 使用IDEA开发Spark程序
- Solution architect's small bag - 5 types of architecture diagrams
- JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
- Chapter II in memory architecture (im-2.2)
猜你喜欢

Solve the problem of installing VMware after uninstalling

JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
![Jerry's more accurate determination of abnormal address [chapter]](/img/ed/a08949bfc63823baf25fd57c324996.png)
Jerry's more accurate determination of abnormal address [chapter]

MapReduce compression

Configuration of LNMP

中职网络安全2022国赛之CVE-2019-0708漏洞利用

/etc/shadow可以破解吗?

微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件

C语言——自定义类型

C language - custom type
随机推荐
Sim Api User Guide(5)
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
Contact between domain name and IP address
微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
What about Jerry's stack overflow? [chapter]
C语言——自定义类型
ansible playbook语法和格式 自动化云计算
Using idea to develop Spark Program
Juc并发编程09——Condition实现源码分析
SQL Server 游标循环表数据
lnmp的配置
59、螺旋矩阵(数组)
ansible 云计算 自动化
第三章 启用和调整IM列存储的大小(IM-3.1)
DBA常用SQL语句(3)- cache、undo、索引和等待事件
206、反转链表(链表)
206. Reverse linked list (linked list)
Six practices of Windows operating system security attack and defense
CSP certification 202203-2 travel plan (multiple solutions)
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
