当前位置:网站首页>[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
2022-07-03 04:50:00 【CodingLJ】
1、 Title Description
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 ).

2、 Algorithm analysis
The level traversal of binary tree :
Store nodes of each layer with queues , Traverse the queue , Add nodes in the queue to the result set , And judge whether the left and right nodes of the current node are empty , Add the results of each layer to the result set .
Queue operations :
① Enter the team :queue.offer();
② Out of the team :queue.poll();
3、 Code implementation
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null){
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
List<Integer> list = new ArrayList<>();
int size = queue.size();
for(int i = 0;i < size;i++){
TreeNode node = queue.poll();
list.add(node.val);
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
res.add(list);
}
return res;
}
}边栏推荐
- 2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
- Day 51 - tree problem
- 普通本科大学生活避坑指南
- 论文阅读_ICD编码_MSMN
- [set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
- Introduction to message queuing (MQ)
- 2.14 summary
- stm32逆向入门
- data2vec! New milestone of unified mode
- General undergraduate college life pit avoidance Guide
猜你喜欢

JVM原理简介
![[luatos sensor] 1 light sensing bh1750](/img/70/07f29e072c0b8630f92ec837fc12d5.jpg)
[luatos sensor] 1 light sensing bh1750

Retirement plan fails, 64 year old programmer starts work again

逆袭大学生的职业规划

联发科技2023届提前批IC笔试(题目)

The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge

FISCO bcos zero knowledge proof Fiat Shamir instance source code
![[XSS bypass - protection strategy] understand the protection strategy and better bypass](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[XSS bypass - protection strategy] understand the protection strategy and better bypass

Internationalization and localization, dark mode and dark mode in compose

Silent authorization login and registration of wechat applet
随机推荐
Youdao cloud notes
General undergraduate college life pit avoidance Guide
有道云笔记
Truncated sentences of leetcode simple questions
Keepalived热备与HAProxy
Market status and development prospect forecast of global button dropper industry in 2022
并发操作-内存交互操作
Interface frequency limit access
Retirement plan fails, 64 year old programmer starts work again
Thesis reading_ Tsinghua Ernie
Market status and development prospects of the global autonomous marine glider industry in 2022
Matplotlib -- save graph
Hj35 serpentine matrix
MC Layer Target
Symbol of array element product of leetcode simple problem
The 19th Zhejiang I. barbecue
Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
移动端——uniapp开发记录(公共请求request封装)
Thesis reading_ Chinese NLP_ ELECTRA
Market status and development prospect prediction of global fermented plant protein industry in 2022