当前位置:网站首页>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 .
边栏推荐
- hbuildx中夜神模拟器的配置以及热更新
- SDP中的SPA
- Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
- Katalon framework tests web (XXI) to obtain element attribute assertions
- Parameterization of controls in katalon
- pytest多进程/多线程执行测试用例
- Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
- Global exposure and roller shutter exposure of industrial cameras
- 指针数组和数组指针
- The maximum expiration time of client secret in azure ad application registration is modified to 2 years
猜你喜欢

Introduction to asynchronous task capability of function calculation - task trigger de duplication

leetcode刷题:二叉树06(对称二叉树)

ctf-pikachu-XSS

Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡

1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
TCP-三次握手和四次挥手简单理解

*. No main manifest attribute in jar

Flink learning 6: programming model

【罗技】m720

Confession code collection, who says program apes don't understand romance
随机推荐
分布式系统:what、why、how
Myslq delete followed by limit
Support the first triggered go ticker
The difference between bagging and boosting in machine learning
[book club issue 13] multimedia processing tool ffmpeg tool set
Go 语言入门很简单:Go 实现凯撒密码
2020 Bioinformatics | TransformerCPI
vim正确加区间注释
Pytest multi process / multi thread execution test case
[microservice openfeign] use openfeign to remotely call the file upload interface
Spa in SDP
idea修改主体颜色
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
I Build a simple microservice project
思考的小记录
【读书会第十三期】多媒体处理工具 FFmpeg 工具集
STM32 external DHT11 display temperature and humidity
Katalon中控件的参数化
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
图解网络:什么是热备份路由器协议HSRP?
