当前位置:网站首页>[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;
}
}边栏推荐
- Two drawing interfaces - 1 Matlab style interface
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
- Handling record of electric skateboard detained by traffic police
- Basic use of Metasploit penetration testing framework
- Mount NFS in kubesphere
- 普通本科大学生活避坑指南
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
- AWS VPC
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
猜你喜欢

JDBC database operation

【XSS绕过-防护策略】理解防护策略,更好的绕过
![[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa](/img/a9/92059db74ccde03b84c69dfce35b37.jpg)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
![[luatos sensor] 2 air pressure bmp180](/img/88/2a6caa5fec95e54e3fb09c74ba8ae6.jpg)
[luatos sensor] 2 air pressure bmp180

Introduction to message queuing (MQ)

Concurrent operation memory interaction

Keepalived热备与HAProxy

Review the old and know the new: Notes on Data Science

Shuttle + Alluxio 加速内存Shuffle起飞

Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
随机推荐
联发科技2023届提前批IC笔试(题目)
有道云笔记
Sdl2 + OpenGL glsl practice (Continued)
Internationalization and localization, dark mode and dark mode in compose
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
I stepped on a foundation pit today
Priv app permission exception
Games101 Lesson 9 shading 3 Notes
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
Review the old and know the new: Notes on Data Science
Market status and development prospect prediction of the global fire extinguisher industry in 2022
Learning practice: comprehensive application of cycle and branch structure (I)
Learning record of arouter principle
Review the configuration of vscode to develop golang
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
Market status and development prospect prediction of global SoC Test Platform Industry in 2022
STM32 reverse entry
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
Apache MPM model and ab stress test
The simple problem of leetcode: dismantling bombs