当前位置:网站首页>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 ,然后深度优先遍历,把结果按层序遍历的索引放上去。
边栏推荐
- Object oriented -- encapsulation, inheritance, polymorphism
- Go 语言入门很简单:Go 实现凯撒密码
- Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
- 01 qemu 启动编译好的镜像 VFS: Unable to mount root fs on unknown-block(0,0)
- Support the first triggered go ticker
- ctf-pikachu-XSS
- Es network layer
- Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
- Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
- EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?
猜你喜欢

Activiti7 task service - process variables (setvariable and setvariablelocal)

Es network layer

There is a problem that the package cannot be parsed in the like project

Mindmanager2022 efficient and easy to use office mind map MindManager

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

Lnk2038 detected a mismatch of "runtimelibrary": the value "md_dynamicrelease" does not match the value "mdd_dynamicdebug" (in main.obj)

毕业设计:设计秒杀电商系统
![[Logitech] m720](/img/bb/44144a1c3907808398c05b3b36962c.png)
[Logitech] m720

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

透过JVM-SANDBOX源码,了解字节码增强技术原理
随机推荐
【罗技】m720
pytest多进程/多线程执行测试用例
A review of reverse reinforcement learning at Virginia Tech (VT)
SQL語句加强練習(MySQL8.0為例)
[paddleseg source code reading] normalize operation of paddleseg transform
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
思考的小记录
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
用于TCP协议交互的TCPClientDemo
VIM add interval annotation correctly
程序员远程办公喜忧参半| 社区征文
mysql数据库的存储
postgresql 用户不能自己创建表格配置
Programmers' telecommuting is mixed | community essay solicitation
pytest多进程/多线程执行测试用例
ctf-pikachu-CSRF
Class summation, shortest row
Unity 绘制弹球和台球的运动轨迹
Interpretation of leveldb source code skiplist
STM32外接DHT11显示温湿度
