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

注意:树的初始化

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. 我的 Kafka 旅程 - Linux下的安装 & 基础命令

    准备工作 安装解压缩工具 tar # 检查是否安装了解压缩工具 tar yum list tar # 如未安装 tar yum install tar -y 安装必备的 java # 检查是否安装了 ...

  2. 第六章:Django 综合篇 - 10:消息框架 message

    在网页应用中,我们经常需要在处理完表单或其它类型的用户输入后,显示一个通知信息给用户. 对于这个需求,Django提供了基于Cookie或者会话的消息框架messages,无论是匿名用户还是认证的用户 ...

  3. ProxySQL 使用情况报错问题汇总及解决办法

    1.ProxySQL Error: connection is locked to hostgroup 2 but trying to reach hostgroup 1 解决方案:登上proxysq ...

  4. 查询参数: Query Parameters

    官方文档地址: https://fastapi.tiangolo.com/zh/tutorial/query-params/ # -*- coding: UTF-8 -*- from fastapi ...

  5. Elasticsearch:Dynamic mapping

    Elasticsearch最重要的功能之一是它试图摆脱你的方式,让你尽快开始探索你的数据. 要索引文档,您不必首先创建索引,定义映射类型和定义字段 - 您只需索引文档,那么index,type和fie ...

  6. Elasticsearch:定制分词器(analyzer)及相关性

    转载自:https://elasticstack.blog.csdn.net/article/details/114278163 在许多的情况下,我们使用现有的分词器已经足够满足我们许多的业务需求,但 ...

  7. Mysql三种日志(binlog,redolog,undolog)的作用和区别

    Mysql有三种很重要的日志也是面试经常涉及到的考点,分别是 binlog .redo log和undo log, 这里面binlog 是server层实现的日志,而redo log 和undo lo ...

  8. 改善C#程序的方法-2 使用TryParse

    一 使用TryParse,而不是Parse 除string外的所有基元类型,都有两个将string类型转型为其本身类型的方法:Parse 和 TryParse. 以double类型为例,这两个方法最简 ...

  9. 二手商城集成jwt认证授权

    ------------恢复内容开始------------ 使用jwt进行认证授权的主要流程 参考博客(https://www.cnblogs.com/RayWang/p/9536524.html) ...

  10. 计算机保研,maybe this is all you need(普通双非学子上岸浙大工程师数据科学项目)

    写在前面 9.28接收了拟录取通知,也终究是尘埃落定了,我人生的又一个阶段也终于结束.面对最终录取结果,或多或少会有所遗憾,但也还是基本达到了预期的目标了. 作为在今年严峻的保研形势下幸存的我,一直想 ...