当前位置:网站首页>【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
边栏推荐
- Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
- Using idea to develop Spark Program
- Construction and traversal of binary tree
- Zhengda international explains what the Dow Jones industrial index is?
- Ansible cloud computing automation
- Swagger2 接口如何导入Postman
- 142、环形链表||
- Chapter II in memory architecture (im-2.2)
- Six practices of Windows operating system security attack and defense
- [untitled]
猜你喜欢
得到知识服务app原型设计比较与实践
【leetcode】102.二叉树的层序遍历
Juc并发编程07——公平锁真的公平吗(源码剖析)
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
101. Symmetric Tree
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
shell脚本免交互
Comparison and practice of prototype design of knowledge service app
Depth selector
Windows installs redis and sets the redis service to start automatically
随机推荐
997、有序数组的平方(数组)
What about Jerry's stack overflow? [chapter]
SQL Server 递归查询上下级
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
ansible playbook语法和格式 自动化云计算
Jinglianwen technology - professional data annotation company and intelligent data annotation platform
Jerry's factors that usually affect CPU performance test results are: [article]
707. Design linked list (linked list)
JVM——》常用命令
DBA common SQL statements (1) - overview information
Zhengda international explains what the Dow Jones industrial index is?
0704、ansible----01
Comparison and practice of prototype design of knowledge service app
Shell script interaction free
Sim Api User Guide(7)
微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
101. Symmetric Tree
IDEA——》每次启动都会Indexing或 scanning files to index
【leetcode】199.二叉树的右视图
19. Delete the penultimate node of the linked list (linked list)