当前位置:网站首页>【leetcode】107. Sequence traversal of binary tree II
【leetcode】107. Sequence traversal of binary tree II
2022-04-23 10:30:00 【Front end corner】
【leetcode】107. Sequence traversal of binary tree II

subject
Give you the root node of the binary tree root , Returns its node value Bottom up sequence traversal . ( That is, from the layer where the leaf node is to the layer where the root node is , Traversal layer by layer from left to right )
Example 1:
Input :root = [3,9,20,null,null,15,7]
Output :[[15,7],[9,20],[3]]
Example 2:
Input :root = [1]
Output :[[1]]
Example 3:
Input :root = []
Output :[]
Tips :
The number of nodes in the tree is in the range [0, 2000] Inside
-1000 <= Node.val <= 1000
Ideas
- Think about this topic and this one 【leetcode】102. Sequence traversal of binary tree What is the difference between the results of topic traversal ? In fact, it's the problem. Just flip the result array .
- The operation of flipping can be carried out when adding single-layer traversal results to the result array , That is, add... From the header of the array .
Code
/** * 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 = [] // Store nodes of each layer
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) // Store the traversal results of the current layer , Add from array header
}
return res
};
Complexity
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( n ) O(n) O(n)
Pay attention to my special column , Update three times a day leetcode Answer key , Get stronger together !
版权声明
本文为[Front end corner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231027441292.html
边栏推荐
- DBA common SQL statements (5) - latch related
- [provincial election joint examination 2022 d2t1] card (state compression DP, FWT convolution)
- Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
- Realizing data value through streaming data integration (5) - flow analysis
- Depth selector
- DBA common SQL statements (1) - overview information
- Ansible cloud computing automation command line compact version
- 图像处理——噪声小记
- Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
- Charles function introduction and use tutorial
猜你喜欢
第120章 SQL函数 ROUND
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
101. Symmetric Tree
JVM——》常用参数
Xshell+Xftp 下载安装步骤
Question bank and answers of Shanghai safety officer C certificate examination in 2022
C语言——自定义类型
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
IDEA——》每次启动都会Indexing或 scanning files to index
/Can etc / shadow be cracked?
随机推荐
CentOS/Linux安装MySQL
Swagger2 自定义参数注解如何不显示
454. Sum of four numbers (hash table)
DBA common SQL statements (3) - cache, undo, index and wait events
203. Remove linked list elements (linked list)
2022 mobile crane driver test question bank simulation test platform operation
Common SQL statements of DBA (6) - daily management
Swagger2 接口如何导入Postman
【无标题】
349. Intersection of two arrays
Arm debugging (1): two methods to redirect printf to serial port in keil
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
Sim Api User Guide(6)
一文看懂 LSTM(Long Short-Term Memory)
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
Chapter 1 Oracle database in memory related concepts (im-1.1)
微信小程序中app.js文件、组件、api
【leetcode】199.二叉树的右视图
解决VMware卸载后再安装出现的问题
1. Sum of two numbers (hash table)