当前位置:网站首页>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 .
边栏推荐
- [book club issue 13] packaging format of video files
- leetcode刷题:二叉树07(二叉树的最大深度)
- 毕业三年,远程半年 | 社区征文
- Flink learning 7: application structure
- Unity draws the trajectory of pinball and billiards
- 网络 - VXLAN
- 【读书会第十三期】多媒体处理工具 FFmpeg 工具集
- Support the first triggered go ticker
- LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
- vue多级路由嵌套怎么动态缓存组件
猜你喜欢
随机推荐
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
软件测试是干什么的 发现缺陷错误,提高软件的质量
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Deep thinking on investment
Global exposure and roller shutter exposure of industrial cameras
Select sorting and bubble sorting template
三年进账35.31亿,这个江西老表要IPO了
leetcode刷题:二叉树04(二叉树的层序遍历)
LevelDB源码解读-SkipList
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
还原窗口位置的微妙之处
Storage of MySQL database
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
[paddleseg source code reading] paddleseg calculation dice
(指针)编写函数void fun(int x,int *pp,int *n)
毕业总结
leetcode刷题:二叉树08(N叉树的最大深度)
(指针)自己写一个比较字符串大小的函数,功能与strcmp类似。
RHCSA 06 - suid, sgid, sticky bit(待补充)
2020 Bioinformatics | TransformerCPI









