当前位置:网站首页>leetcode刷题:二叉树04(二叉树的层序遍历)
leetcode刷题:二叉树04(二叉树的层序遍历)
2022-07-04 03:51:00 【涛涛英语学不进去】
102.二叉树的层序遍历
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
乍一看挺复杂,仔细做一下,一招秒。
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. 二叉树的层序遍历 **/
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;
}
//入队
deque.offer(root);
while (!deque.isEmpty()) {
int size=deque.size();
List<Integer> floor = new ArrayList();
while (size > 0) {
//每次遍历一层
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;
}
}
层序遍历,大 list
包小 list 。 每个小 list 为一层。
一层一层来,记录当前队列的长度,就是上一层有多少元素,这次就弹出多少次,把新元素继续加在队列尾部即可。
看了一下题解,我的属于广度优先遍历,还有深度优先遍历。
/** * 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) {
//当层级增加时,list的Item也增加,利用List的索引值进行层级结点
List<Integer> item = new ArrayList<>();
resList.add(item);
}
resList.get(deep - 1).add(node.val);
checkFun01(node.left, deep);
checkFun01(node.right, deep);
}
}
有一说一我看到不太懂,有点像先准备一个 List ,然后深度优先遍历,把结果按层序遍历的索引放上去。
边栏推荐
- laravel admin里百度编辑器自定义路径和文件名
- Sales management system of lightweight enterprises based on PHP
- 华为云鲲鹏工程师培训(广西大学)
- ctf-pikachu-XSS
- 2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
- [paddleseg source code reading] normalize operation of paddleseg transform
- The difference between bagging and boosting in machine learning
- Idea modify body color
- “软硬皆施”,助力建成新型云计算数据中心
- Three years of graduation, half a year of distance | community essay solicitation
猜你喜欢
Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool
Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)
Confession code collection, who says program apes don't understand romance
Penetration practice - sqlserver empowerment
The maximum expiration time of client secret in azure ad application registration is modified to 2 years
Flink学习6:编程模型
MySQL one master multiple slaves + linear replication
三年进账35.31亿,这个江西老表要IPO了
如何有效远程办公之我见 | 社区征文
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
随机推荐
Illustrated network: what is the hot backup router protocol HSRP?
Redis cluster uses Lua script. Lua script can also be used for different slots
I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
Go 语言入门很简单:Go 实现凯撒密码
SDP中的SPA
ctf-pikachu-XSS
How was my life in 2021
【读书会第十三期】视频文件的封装格式
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Graduation summary
Reduce function under functools
Katalon使用script实现查询List大小
VIM add interval annotation correctly
Deep thinking on investment
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
【罗技】m720
Getting started with the go language is simple: go implements the Caesar password
干货!基于GAN的稀有样本生成
Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool
Objective-C description method and type method