描述
给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)

注意:树的初始化

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 按之字形顺序打印二叉树的更多相关文章

  1. 58按之字形顺序打印二叉树 +队列访问使用front和back,栈才是top

    题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推.   思路:最暴力的方法就是使用队列进行层次遍 ...

  2. Appium最新的服务器初始化参数(Capability)【截止11月29日,后续最新的可以看github】

    键 描述 值 automationName 自动化测试的引擎 Appium (默认)或者 Selendroid platformName 使用的手机操作系统 iOS, Android, 或者 Fire ...

  3. STL双端队列 deque

    头文件:#include<deque> 构造方法: ①.创建一个没有任何元素的双端队列:deque<type> deq ②.用另一个类型相同双端队列初始化该双端队列:deque ...

  4. 并发编程-concurrent指南-阻塞双端队列BlockingDeque

    java.util.concurrent 包里的 BlockingDeque 接口表示一个线程安放入和提取实例的双端队列. BlockingDeque 类是一个双端队列,在不能够插入元素时,它将阻塞住 ...

  5. 剑指 Offer 32 - III. 从上到下打印二叉树 III + 双端队列使用 + 蛇形打印层次遍历序列 + 正倒序输出

    剑指 Offer 32 - III. 从上到下打印二叉树 III Offer_32_3 题目详情 题解分析 本题我想的比较复杂,其实题目的要求只是需要遍历的结果逆序和正序交替,这个其实可以使用Coll ...

  6. BZOJ 2457 - 双端队列 - [思维题]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2457 Description Sherry现在碰到了一个棘手的问题,有N个整数需要排序. ...

  7. C#LeetCode刷题之#641-设计循环双端队列(Design Circular Deque)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4132 访问. 设计实现双端队列. 你的实现需要支持以下操作: M ...

  8. [BZOJ2457][BeiJing2011]双端队列 (单调性)

    正如lyd所说,和数据结构本身没什么太大关联 题意 中文题面   Sherry现在碰到了一个棘手的问题,有N个整数需要排序.        Sherry手头能用的工具就是若干个双端队列.        ...

  9. BZOJ 2457 [BeiJing2011] 双端队列

    2457: [BeiJing2011]双端队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 340  Solved: 167[Submit][Sta ...

  10. lintcode 滑动窗口的最大值(双端队列)

    题目链接:http://www.lintcode.com/zh-cn/problem/sliding-window-maximum/# 滑动窗口的最大值 给出一个可能包含重复的整数数组,和一个大小为  ...

随机推荐

  1. 使用 Elastic 技术栈构建 K8S 全栈监控 -1:搭建 ElasticSearch 集群环境

    文章转载自:https://www.qikqiak.com/post/k8s-monitor-use-elastic-stack-1/ 操作步骤 kubectl create ns elastic k ...

  2. 应用健康: Liveness 与 Readiness

    文章转载自:https://www.kuboard.cn/learning/k8s-intermediate/workload/pod-health.html 介绍 Liveness 指针是存活指针, ...

  3. Elasticsearch:ICU分词器介绍

    ICU Analysis插件是一组将Lucene ICU模块集成到Elasticsearch中的库. 本质上,ICU的目的是增加对Unicode和全球化的支持,以提供对亚洲语言更好的文本分割分析. 从 ...

  4. Elastic: 如何在阿里云上构建Elastic集群

  5. 轻松绕过waf,内网技术,Cobalt Strike4.4远控木马绕waf流量监控

    DNS隧道技术可以解决运控木马无法上线的问题,waf,防火墙对tcp,http,https等端口有流量检测,这个时候我们就可以使用隧道技术,让cs木马走DNS隧道,不仅可以检测不到而且也是一种反溯源的 ...

  6. Java线程同步的四种方式详解(建议收藏)

    ​ Java线程同步属于Java多线程与并发编程的核心点,需要重点掌握,下面我就来详解Java线程同步的4种主要的实现方式@mikechen 目录 什么是线程同步 线程同步的几种方式 1.使用sync ...

  7. BinaryBombs(二进制炸弹实验)

    实验介绍 使用所学知识拆除Binary Bombs来增强对程序的机器级表示.汇编语言.调试器和逆向工程等理解. Binary Bombs(二进制炸弹)是一个可执行程序,是C语言编译链接成的,包含pha ...

  8. vscode markdown WYSIWYG 所见即所得编辑和预览

    一直使用Typora编写markdown,随着vscode在工作中使用的越来越多,产生了一个想法:能不能在vscode中写markdown,减少软件的成本? 可是vscode官方自带的的markdow ...

  9. 微信电脑版DAT文件转图片工具

    一键批量将微信聊天接受到的加密存储DAT图片文件转化为普通图片. 通过查看转化后的图片,您可以: (1)清理无用的历史图片,节省电脑硬盘存储空间. (2)恢复寻找重要照片资料. 下载地址:点此下载 微 ...

  10. 基于YOLO和PSPNet的目标检测与语义分割系统(python)

    基于YOLO和PSPNet的目标检测与语义分割系统 源代码地址 概述 这是我的本科毕业设计 它的主要功能是通过YOLOv5进行目标检测,并使用PSPNet进行语义分割. 本项目YOLOv5部分代码基于 ...