当前位置:网站首页>Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
Leetcode skimming: binary tree 04 (sequence traversal of binary tree)
2022-07-04 04:20:00 【Taotao can't learn English】
102. Sequence traversal of binary tree
Give you a binary tree , Please return to its button Sequence traversal The resulting node value . ( That is, layer by layer , Access all nodes from left to right ).
At first glance, it looks complicated , Do it carefully , One move per second .
package com.programmercarl.tree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/** * @ClassName LevelOrder * @Descriotion TODO * @Author nitaotao * @Date 2022/7/3 11:55 * @Version 1.0 * https://leetcode.cn/problems/binary-tree-level-order-traversal/ * 102. Sequence traversal of binary tree **/
public class LevelOrder {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Deque<TreeNode> deque = new ArrayDeque();
if (root == null) {
return result;
}
// The team
deque.offer(root);
while (!deque.isEmpty()) {
int size=deque.size();
List<Integer> floor = new ArrayList();
while (size > 0) {
// Traverse one layer at a time
TreeNode node = deque.poll();
floor.add(node.val);
if (node.left != null) {
deque.offer(node.left);
}
if (node.right != null) {
deque.offer(node.right);
}
size--;
}
result.add(floor);
}
return result;
}
}
Sequence traversal , Big list
Bao Xiao list . Every little list For the first floor .
Layer by layer , Record the length of the current queue , That is, how many elements are there on the upper layer , How many times will it pop up this time , Add new elements to the end of the queue .
Look at the solution , Mine belongs to breadth first traversal , And depth first traversal .
/** * 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>> resList = new ArrayList<List<Integer>>();
public List<List<Integer>> levelOrder(TreeNode root) {
checkFun01(root, 0);
return resList;
}
public void checkFun01(TreeNode node, Integer deep) {
if (node == null) {
return;
}
deep++;
if (resList.size() < deep) {
// As the hierarchy increases ,list Of Item Also increase , utilize List Index value of the hierarchy node
List<Integer> item = new ArrayList<>();
resList.add(item);
}
resList.get(deep - 1).add(node.val);
checkFun01(node.left, deep);
checkFun01(node.right, deep);
}
}
I don't understand what I see , It's a bit like preparing one first List , Then depth first traversal , Put the index of the result according to the sequence traversal .
边栏推荐
- leetcode刷题:二叉树07(二叉树的最大深度)
- Myslq delete followed by limit
- [book club issue 13] multimedia processing tool ffmpeg tool set
- Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
- 【罗技】m720
- RHCSA 06 - suid, sgid, sticky bit(待补充)
- Programmers' telecommuting is mixed | community essay solicitation
- (指针)编写函数void fun(int x,int *pp,int *n)
- dried food! Generation of rare samples based on GaN
- Select sorting and bubble sorting template
猜你喜欢
96% of the collected traffic is prevented by bubble mart of cloud hosting
北漂程序员,月薪20K,一年攒15W,正常吗?
Go 语言入门很简单:Go 实现凯撒密码
Katalon framework test web (XXVI) automatic email
Parameterization of controls in katalon
leetcode刷题:二叉树05(翻转二叉树)
Activiti7 task service - process variables (setvariable and setvariablelocal)
Brief explanation of depth first search (with basic questions)
ctf-pikachu-XSS
Evolution of MySQL database architecture
随机推荐
Common methods of threads
Katalon使用script实现查询List大小
【CSRF-01】跨站请求伪造漏洞基础原理及攻防
Katalon中控件的参数化
02 ls 命令的具体实现
Interpretation of leveldb source code skiplist
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
Redis cluster uses Lua script. Lua script can also be used for different slots
ctf-pikachu-XSS
如何远程办公更有效率 | 社区征文
线程常用的方法
Go 语言入门很简单:Go 实现凯撒密码
(指针)编写函数void fun(int x,int *pp,int *n)
【webrtc】m98 ninja 构建和编译指令
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
Smart subway | cloud computing injects wisdom into urban subway transportation
[microservice openfeign] use openfeign to remotely call the file upload interface
Small record of thinking
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
【罗技】m720