当前位置:网站首页>[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;
}
}边栏推荐
- Basic use of Metasploit penetration testing framework
- 7. Integrated learning
- The current market situation and development prospect of the global gluten tolerance test kit industry in 2022
- 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
- MC Layer Target
- Number of uniform strings of leetcode simple problem
- Market status and development prospect prediction of the global fire extinguisher industry in 2022
- Market status and development prospect prediction of the global fire alarm sensor industry in 2022
- C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
- The least operation of leetcode simple problem makes the array increment
猜你喜欢

Network security textual research recommendation

Retirement plan fails, 64 year old programmer starts work again

Do you know UVs in modeling?

Basic use of Metasploit penetration testing framework

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills

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

Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute

Two drawing interfaces - 1 Matlab style interface

Web security - CSRF (token)
![[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
随机推荐
Handling record of electric skateboard detained by traffic police
联发科技2023届提前批IC笔试(题目)
Reptile exercise 03
Web security - CSRF (token)
Market status and development prospect forecast of global heat curing adhesive industry in 2022
【SQL注入】联合查询(最简单的注入方法)
stm32逆向入门
Review the configuration of vscode to develop golang
Analysis of proxy usage of ES6 new feature
STM32 reverse entry
Market status and development prospect prediction of global SoC Test Platform Industry in 2022
4 years of experience to interview test development, 10 minutes to end, ask too
【XSS绕过-防护策略】理解防护策略,更好的绕过
Mount NFS in kubesphere
Auman Galaxy new year of the tiger appreciation meeting was held in Beijing - won the double certification of "intelligent safety" and "efficient performance" of China Automotive Research Institute
Career planning of counter attacking College Students
论文阅读_中文医疗模型_ eHealth
Interface frequency limit access
Pyqt control part (II)
The least operation of leetcode simple problem makes the array increment