当前位置:网站首页>【leetcode】102. Sequence traversal of binary tree
【leetcode】102. Sequence traversal of binary tree
2022-04-23 10:30:00 【Front end corner】
【leetcode】102. Sequence traversal of binary tree
subject
Give you the root node of the binary tree root , Returns the Sequence traversal . ( That is, layer by layer , Access all nodes from left to right ).
Example 1:
Input :root = [3,9,20,null,null,15,7]
Output :[[3],[9,20],[15,7]]
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

- The sequence traversal of binary tree is traversing layer by layer from left to right , Using the data structure of queue, which is first in first out, we can complete .
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 levelOrder = 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.push(curLevel) // Store the traversal results of the current layer
}
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/202204231027441343.html
边栏推荐
- JVM——》常用命令
- Swagger2 接口如何导入Postman
- Contact between domain name and IP address
- Comparison and practice of prototype design of knowledge service app
- [provincial election joint examination 2022 d2t1] card (state compression DP, FWT convolution)
- Windows installs redis and sets the redis service to start automatically
- 转:毛姆:阅读是一座随身携带的避难所
- 19. Delete the penultimate node of the linked list (linked list)
- Arm debugging (1): two methods to redirect printf to serial port in keil
- Download and installation steps of xshell + xftp
猜你喜欢

Swagger2 接口如何导入Postman

【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)

JVM——》常用命令

Zhengda international explains what the Dow Jones industrial index is?

Yarn core parameter configuration

Operation of 2022 tea artist (primary) test question simulation test platform

Sim Api User Guide(6)

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

Sim Api User Guide(5)

SQL Server 递归查询上下级
随机推荐
Jerry's users how to handle events in the simplest way [chapter]
Realize data value through streaming data integration (1)
解决VMware卸载后再安装出现的问题
59. Spiral matrix (array)
第120章 SQL函数 ROUND
Swagger2 接口如何导入Postman
Sim Api User Guide(6)
DBA常用SQL语句 (5) - Latch 相关
Swagger2 自定义参数注解如何不显示
Sim Api User Guide(7)
基于PyQt5实现弹出任务进度条功能示例
二叉树的构建和遍历
202. Happy number
Art template template engine
142. Circular linked list||
Go language practice mode - functional options pattern
Realizing data value through streaming data integration (5) - stream processing
Example of pop-up task progress bar function based on pyqt5
【leetcode】107.二叉树的层序遍历II
2022 mobile crane driver test question bank simulation test platform operation
