【每日一题】【(双端)队列初始化&工具类&层次遍历】2022年1月29日-NC14 按之字形顺序打印二叉树
描述
给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)


注意:树的初始化
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
类似:链表的初始化
public class LinkNode {
int val;
LinkNode next = null;
public LinkNode(int val) {
this.val = val;
}
}
方法1:普通队列进行层次遍历,隔层使用一次工具类进行翻转
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val;
} }
*/
public class Solution {
/**
使用队列实现层次遍历(先进先出)
层次遍历判断每一层元素个数不用递归,可以直接看queue的size
**/
public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
if(pRoot == null) {
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(pRoot);
int index = 0;
while(!queue.isEmpty()) {
int size = queue.size();
ArrayList<Integer> arr = new ArrayList<>();
for(int i = 0; i < size; i++) {
TreeNode node = queue.poll();
arr.add(node.val);
if(node.left != null) {
queue.offer(node.left);
}
if(node.right != null) {
queue.offer(node.right);
}
}
if(index % 2 != 0) {
Collections.reverse(arr);
}
index++;
res.add(arr);
}
return res;
}
}
注意:队列的初始化是new LinkedList<>();
方法2:使用ArrayList的重载add函数,往前面插入和往后面插入
import java.util.*; /*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val;
} }
*/
public class Solution {
/**
使用队列实现层次遍历(先进先出)
层次遍历判断每一层元素个数不用递归,可以直接看queue的size
左右可以使用双端队列实现
**/
public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
if(pRoot == null) {
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(pRoot);
int index = 0;
while(!queue.isEmpty()) {
int size = queue.size();
ArrayList<Integer> arr = new ArrayList<>();
for(int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if(index % 2 == 0) {
arr.add(node.val);
} else {
arr.add(0, node.val);
}
if(node.left != null) {
queue.offer(node.left);
}
if(node.right != null) {
queue.offer(node.right);
}
}
index++;
res.add(arr);
}
return res;
}
}
方法3:使用双端队列,每次从不同的方向出队&入队
import java.util.*; /*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
public class Solution {
/**
使用双端队列实现
**/
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer> > res = new ArrayList<>();
if(pRoot == null) {
return res;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(pRoot);
int index = 0;
while(!queue.isEmpty()) {
int size = queue.size();
ArrayList<Integer> arr = new ArrayList<>();
TreeNode node;
for(int i = 0; i < size; i++) {
if(index % 2 == 0) {
node = queue.pollFirst();
arr.add(node.val);
if(node.left != null) {
queue.offerLast(node.left);
}
if(node.right != null) {
queue.offerLast(node.right);
}
} else {
node = queue.pollLast();
arr.add(node.val);
if(node.right != null) {
queue.offerFirst(node.right);
}
if(node.left != null) {
queue.offerFirst(node.left);
}
}
}
index++;
res.add(arr);
}
return res;
} }
【每日一题】【(双端)队列初始化&工具类&层次遍历】2022年1月29日-NC14 按之字形顺序打印二叉树的更多相关文章
- 58按之字形顺序打印二叉树 +队列访问使用front和back,栈才是top
题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 思路:最暴力的方法就是使用队列进行层次遍 ...
- Appium最新的服务器初始化参数(Capability)【截止11月29日,后续最新的可以看github】
键 描述 值 automationName 自动化测试的引擎 Appium (默认)或者 Selendroid platformName 使用的手机操作系统 iOS, Android, 或者 Fire ...
- STL双端队列 deque
头文件:#include<deque> 构造方法: ①.创建一个没有任何元素的双端队列:deque<type> deq ②.用另一个类型相同双端队列初始化该双端队列:deque ...
- 并发编程-concurrent指南-阻塞双端队列BlockingDeque
java.util.concurrent 包里的 BlockingDeque 接口表示一个线程安放入和提取实例的双端队列. BlockingDeque 类是一个双端队列,在不能够插入元素时,它将阻塞住 ...
- 剑指 Offer 32 - III. 从上到下打印二叉树 III + 双端队列使用 + 蛇形打印层次遍历序列 + 正倒序输出
剑指 Offer 32 - III. 从上到下打印二叉树 III Offer_32_3 题目详情 题解分析 本题我想的比较复杂,其实题目的要求只是需要遍历的结果逆序和正序交替,这个其实可以使用Coll ...
- BZOJ 2457 - 双端队列 - [思维题]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2457 Description Sherry现在碰到了一个棘手的问题,有N个整数需要排序. ...
- C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4132 访问. 设计实现双端队列. 你的实现需要支持以下操作: M ...
- [BZOJ2457][BeiJing2011]双端队列 (单调性)
正如lyd所说,和数据结构本身没什么太大关联 题意 中文题面 Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若干个双端队列. ...
- BZOJ 2457 [BeiJing2011] 双端队列
2457: [BeiJing2011]双端队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 340 Solved: 167[Submit][Sta ...
- lintcode 滑动窗口的最大值(双端队列)
题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为 ...
随机推荐
- MinIO多租户(Multi-tenant)部署指南
官方文档地址:http://docs.minio.org.cn/docs/master/multi-tenant-minio-deployment-guide 单机部署 在单台机器上托管多个租户,为每 ...
- KubeOperator版本升级后有关nexus组件的密码问题说明
KO升级后,会覆盖原版本的nexus持久化文件,nexus密码会还原为默认密码admin123 在KO升级成功并用默认密码登录成功后,若想修改nexus密码,采用如下方式 1.先用默认密码登录nexu ...
- 使用traefik进行金丝雀发布
文章转载自:https://mp.weixin.qq.com/s/nMMN7hAJK6SFn1V1YyxvHA 在 Traefik 2.0 中以服务负载均衡的形式进行了支持.可以将服务负载均衡器看成负 ...
- 使用shell做http web接口,可以传递参数--废弃
此文章废弃,参考另一篇 参考网址: https://me.jinchuang.org/archives/114.html https://www.cnblogs.com/jinchuang/p/142 ...
- 使用FastDFS打造一款高可用的分布式文件系统
FastDFS 介绍 参考: http://www.oschina.net/p/fastdfs FastDFS 是一个开源的分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文 ...
- 1_Html
一. 引言 1.1 HTML概念 网页, 是网站中的一个页面, 是构成网站的基本元素, 是承载各种网站应用的平台. 通俗的说, 网站就是由网页组成的, 通常我们看到的网页都是以html或html后缀结 ...
- CSS基础-关于CSS注释的添加
在 CSS 中增加注释很简单,所有被放在/*和*/分隔符之间的文本信息都被称为注释. CSS 只有一种注释,不管是多行注释还是单行注释,都必须以/*开始.以*/结束,中间加入注释内容. 1.注释放在样 ...
- BUUCTF-PWN-第一页writep(32题)
温故而知新,可以为师矣.所以花了几天时间重新做了下 buuctf 的 pwn 题,先发下第一页共 32 题的题解.还有如果题解都很详细那么本文就太长了,写起来也浪费时间,所以比较简单的题就直接丢 ex ...
- 『现学现忘』Git分支 — 38、Git分支介绍
目录 1.Git分支简介 2.Git分支与SVN分支的区别 3.工作中为什么要使用分支 4.Git分支管理的好处 1.Git分支简介 几乎所有的版本控制系统都以某种形式支持分支. 使用分支意味着,你可 ...
- 只能用于文本与图像数据?No!看TabTransformer对结构化业务数据精准建模
作者:韩信子@ShowMeAI 深度学习实战系列:https://www.showmeai.tech/tutorials/42 TensorFlow 实战系列:https://www.showmeai ...