当前位置:网站首页>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 .
边栏推荐
- How to telecommute more efficiently | community essay solicitation
- JS实现文字滚动 跑马灯效果
- Flink learning 6: programming model
- [book club issue 13] packaging format of video files
- Restore the subtlety of window position
- 1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
- 2020 Bioinformatics | TransformerCPI
- Parameterization of controls in katalon
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- 10 reasons for not choosing to use free virtual hosts
猜你喜欢

02 ls 命令的具体实现

ctf-pikachu-XSS

Activiti7 task service - process variables (setvariable and setvariablelocal)

软件测试是干什么的 发现缺陷错误,提高软件的质量

Confession code collection, who says program apes don't understand romance

Unity draws the trajectory of pinball and billiards

Idea configuration 360zip open by default -- external tools

dried food! Generation of rare samples based on GaN

leetcode刷题:二叉树06(对称二叉树)
TCP-三次握手和四次挥手简单理解
随机推荐
*. No main manifest attribute in jar
如何有效远程办公之我见 | 社区征文
支持首次触发的 Go Ticker
TCP-三次握手和四次挥手简单理解
[book club issue 13] packaging format of video files
一位毕业生的自我分享
LevelDB源码解读-SkipList
(指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
干货!基于GAN的稀有样本生成
【华为云IoT】读书笔记之《万物互联:物联网核心技术与安全》第3章(上)
One click compilation and deployment of MySQL
Graduation summary
02 ls 命令的具体实现
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
ctf-pikachu-CSRF
毕业总结
网络 - VXLAN
Confession code collection, who says program apes don't understand romance
SDP中的SPA
深度优先搜索简要讲解(附带基础题)
