当前位置:网站首页>[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;
}
}
边栏推荐
- 【XSS绕过-防护策略】理解防护策略,更好的绕过
- Integration of Android high-frequency interview questions (including reference answers)
- Market status and development prospect prediction of the global fire alarm sensor industry in 2022
- Priv app permission exception
- Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
- MediaTek 2023 IC written examination approved in advance (topic)
- 2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
- Do you know UVs in modeling?
- 消息队列(MQ)介绍
- STM32 reverse entry
猜你喜欢
[USACO 2009 Dec S]Music Notes
Leetcode simple question: check whether two string arrays are equal
String matching: find a substring in a string
2022 P cylinder filling test content and P cylinder filling simulation test questions
Silent authorization login and registration of wechat applet
论文阅读_中文医疗模型_ eHealth
Pyqt control part (II)
I stepped on a foundation pit today
Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
JDBC database operation
随机推荐
Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
Priv app permission exception
Market status and development prospect forecast of global heat curing adhesive industry in 2022
[SQL injection point] location and judgment of the injection point
Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
第十九届浙江省 I. Barbecue
Leetcode simple question: check whether the string is an array prefix
Learning practice: comprehensive application of cycle and branch structure (I)
Do you know UVs in modeling?
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
Sdl2 + OpenGL glsl practice (Continued)
[SQL injection] joint query (the simplest injection method)
Truncated sentences of leetcode simple questions
Network security textual research recommendation
Thesis reading_ Tsinghua Ernie
联发科技2023届提前批IC笔试(题目)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Hj35 serpentine matrix
Handling record of electric skateboard detained by traffic police
[set theory] binary relation (example of binary relation on a | binary relation on a)