当前位置:网站首页>[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;
}
}边栏推荐
- Wechat applet waterfall flow and pull up to the bottom
- Shell script -- condition judgment
- The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
- Thesis reading_ Chinese medical model_ eHealth
- Shuttle + Alluxio 加速内存Shuffle起飞
- Market status and development prospect prediction of global neutral silicone sealant industry in 2022
- Network security textual research recommendation
- Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
- Market status and development prospect prediction of global fermented plant protein industry in 2022
- 2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
猜你喜欢

The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping

2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination

MediaTek 2023 IC written examination approved in advance (topic)

Thesis reading_ Chinese medical model_ eHealth
![[tools run SQL blind note]](/img/c3/86db4568b221d2423914990a88eec2.png)
[tools run SQL blind note]

Concurrent operation memory interaction

Introduction to message queuing (MQ)

ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation

FISCO bcos zero knowledge proof Fiat Shamir instance source code

Apache MPM model and ab stress test
随机推荐
I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
document. The problem of missing parameters of referer is solved
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
The 19th Zhejiang I. barbecue
Internationalization and localization, dark mode and dark mode in compose
MC Layer Target
Two drawing interfaces - 1 Matlab style interface
Priv-app permission异常
Wechat applet waterfall flow and pull up to the bottom
Keepalived热备与HAProxy
Shuttle + Alluxio 加速内存Shuffle起飞
Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
MC Layer Target
论文阅读_清华ERNIE
移动端——uniapp开发记录(公共请求request封装)
On typescript and grammar
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
[set theory] binary relationship (definition field | value field | inverse operation | inverse synthesis operation | restriction | image | single root | single value | nature of synthesis operation)