LeetCode 二叉树的锯齿形层次遍历
第103题
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回锯齿形层次遍历如下:
[
[3],
[20,9],
[15,7]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal
解题思路
基于二叉树层序遍历改一点代码即可.二叉树层序遍历
- 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的抽象数据类型。双端队列中的元素可以从两端弹出,插入和删除操作限定在队列的两边进行。
- 本题和普通的层序遍历区别在于如何正确的选取加入子节点的顺序以及先后.
- 利用双端队列,左进的时候,右出;右进的时候,左出。正好满足一层正序遍历,一层逆序遍历。
代码实现
class Solution103_1 {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
return BFS(root);
}
List<List<Integer>> BFS(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.addLast(root);
List<List<Integer>> result = new LinkedList<>();
boolean reverse = true;
while (!deque.isEmpty()) {
int size = deque.size();
List<Integer> subResult = new LinkedList<>();
for (int i = 0; i < size; i++) {
TreeNode node;
if (reverse) {
//头出
node = deque.pollFirst();
//尾进
if (node.left != null) {
deque.addLast(node.left);
}
if (node.right != null) {
deque.addLast(node.right);
}
} else {
//尾出
node = deque.pollLast();
//头进
if (node.right != null) {
deque.addFirst(node.right);
}
if (node.left != null) {
deque.addFirst(node.left);
}
}
subResult.add(node.val);
}
result.add(subResult);
reverse = !reverse;
}
return result;
}
}
完整代码
public class Sub103 {
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
// TreeNode root = new TreeNode(1);
// root.left = new TreeNode(2);
// root.right = new TreeNode(3);
// root.left.left = new TreeNode(4);
// root.right.right = new TreeNode(5);
Solution103_1 solution = new Solution103_1();
List<List<Integer>> list = solution.zigzagLevelOrder(root);
for (List<Integer> subList : list) {
System.out.println(subList.toString());
}
}
}
class Solution103_1 {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
return BFS(root);
}
List<List<Integer>> BFS(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.addLast(root);
List<List<Integer>> result = new LinkedList<>();
boolean reverse = true;
while (!deque.isEmpty()) {
int size = deque.size();
List<Integer> subResult = new LinkedList<>();
for (int i = 0; i < size; i++) {
TreeNode node;
if (reverse) {
//头出
node = deque.pollFirst();
//尾进
if (node.left != null) {
deque.addLast(node.left);
}
if (node.right != null) {
deque.addLast(node.right);
}
} else {
//尾出
node = deque.pollLast();
//头进
if (node.right != null) {
deque.addFirst(node.right);
}
if (node.left != null) {
deque.addFirst(node.left);
}
}
subResult.add(node.val);
}
result.add(subResult);
reverse = !reverse;
}
return result;
}
}
资料
LeetCode 二叉树的锯齿形层次遍历的更多相关文章
- LeetCode:二叉树的锯齿形层次遍历【103】
LeetCode:二叉树的锯齿形层次遍历[103] 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- Java实现 LeetCode 103 二叉树的锯齿形层次遍历
103. 二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null ...
- leetcode 102. 二叉树的层次遍历 及 103. 二叉树的锯齿形层次遍历
102. 二叉树的层次遍历 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / ...
- LeetCode103. 二叉树的锯齿形层次遍历
103. 二叉树的锯齿形层次遍历 描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 示例 例如,给定二叉树: [3,9,2 ...
- 【二叉树-BFS系列1】二叉树的右视图、二叉树的锯齿形层次遍历
题目 199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, ...
- lintcode二叉树的锯齿形层次遍历 (双端队列)
题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...
- lintcode: 二叉树的锯齿形层次遍历
题目 二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / \ ...
- LintCode-71.二叉树的锯齿形层次遍历
二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 返回其锯齿形的层次 ...
随机推荐
- linux创建文件名添加当前系统日期时间的方法
使用`date +%y%m%d` Example: mkdir `date +%y%m%d` tar cfvz /tmp/bak.`date +%y%m%d`.tar.gz /etc YmdHM代表年 ...
- 【Java】Java中的final关键字和static
0.概述 final关键字表示是不可变的: 下面分别从属性(字段).方法.类中进行说明: 1.属性(or字段),表示常量 final声明在属性(or字段)中,表示常量,有两种初始化方法,1是在声明时直 ...
- Netty学习——Google Protobuf使用方式分析和环境搭建
Google Protobuf使用方式分析 在RPC框架中,Google Protobuf是很常用的一个库,和Apache Thrift 是同款的用于进行序列化的第三方库.原理都是大同小异,无非就是使 ...
- 基于vue的cropper插件编写分享
目录 简介 实现功能 实现原理 github地址:https://github.com/yinzhida/vue-crop git clone: https://github.com/yinzhida ...
- volatile在外设寄存器基地址定义时的作用
volatile,作用就是告诉编译器不要因优化而省略此指令,必须每次都直接读写其值,这样就能确保每次读或者写寄存器都真正执行到位.——野火
- CF372C Watching Fireworks is Fun(单调队列优化DP)
A festival will be held in a town's main street. There are n sections in the main street. The sectio ...
- BZOJ 3108: [cqoi2013]图的逆变换
3108: [cqoi2013]图的逆变换 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 627 Solved: 415[Submit][Statu ...
- 5分钟教你看大神操作keepalived服务
第11章 高可用服务(keepalived)的配置 11.1 高可用服务的概念 11.1.1 高可用服务总体概念 为了解决单点故障 减轻服务器的压力 11.1.2 高可用keepalived的概念 为 ...
- 怎么定义一个自己的vue组件
1.在src文件夹中创建一个hello文件夹,然后创建hello.js和hello.vue 2.hello.vue代码如下 <template> <button>这是hello ...
- ARTS-S EN0001-In tech race with China, US universities may lose a vital edge
原文 The U.S. is still out in front of global rivals when it comes to innovation, but American univers ...