当前位置:网站首页>【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
边栏推荐
- JUC concurrent programming 09 -- source code analysis of condition implementation
- 707. Design linked list (linked list)
- Arm debugging (1): two methods to redirect printf to serial port in keil
- Comparison and practice of prototype design of knowledge service app
- Art template template engine
- Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
- 通过流式数据集成实现数据价值(5)- 流分析
- Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
- Jerry's users how to handle events in the simplest way [chapter]
- What are the system events of Jerry's [chapter]
猜你喜欢
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
Windows installs redis and sets the redis service to start automatically
Download and installation steps of xshell + xftp
Question bank and answers of Shanghai safety officer C certificate examination in 2022
2022 mobile crane driver test question bank simulation test platform operation
/etc/shadow可以破解吗?
精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
Solution architect's small bag - 5 types of architecture diagrams
得到知识服务app原型设计比较与实践
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
随机推荐
Configuration of LNMP
59. Spiral matrix (array)
Chapter 3 enable and adjust the size of IM column storage (im-3.1)
C language - custom type
Realizing data value through streaming data integration (5) - stream processing
得到知识服务app原型设计比较与实践
What about Jerry's stack overflow? [chapter]
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
Leetcode22:括号生成
得到知识服务app原型设计比较与实践
精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
【leetcode】102.二叉树的层序遍历
通过流式数据集成实现数据价值(5)- 流分析
Go language practice mode - functional options pattern
Reading integrity monitoring techniques for vision navigation systems - 3 background
微信小程序中app.js文件、组件、api
基于PyQt5实现弹出任务进度条功能示例
域名和IP地址的联系
Jerry's users how to handle events in the simplest way [chapter]
2022 mobile crane driver test question bank simulation test platform operation