Binary Tree Zigzag Level Order Traversal(z字形打印二叉树)
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
这题仔细分析:按照之前层序遍历,是使用一个队列,但是这里使用队列不太好解决。
队列不行,就考虑栈,一个栈也不行,因为是深度遍历的。
这里使用两个栈,一个栈放奇数层,一个栈放偶数层。遍历第一个栈,它的输出是从左往右,依次将它的左右节点放到第二个栈中,接下来遍历第二个栈,输出就是从右往左了,然后将其右左节点依次放到第一个栈中,这样再遍历第一个栈时,输出就是从左往右了。。。
代码如下:注意其中的一个细节
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(root==null) return res;
Stack<TreeNode> s1=new Stack<>();
Stack<TreeNode> s2=new Stack<>();
List<Integer> list=new ArrayList<>();
s1.push(root);
while(!s1.isEmpty()||!s2.isEmpty()){
if(!s1.isEmpty()){
int size=s1.size();
for(int i=0;i<size;i++){
TreeNode node=s1.pop();
list.add(node.val);
if(node.left!=null) s2.push(node.left);
if(node.right!=null) s2.push(node.right);
}
res.add(list);
list=new ArrayList<>();
}
if(!s2.isEmpty()){
int size=s2.size();
for(int i=0;i<size;i++){
TreeNode node=s2.pop();
list.add(node.val);
if(node.right!=null) s1.push(node.right);
if(node.left!=null) s1.push(node.left);
}
res.add(list);
list=new ArrayList<>();
}
}
return res;
}
}
Binary Tree Zigzag Level Order Traversal(z字形打印二叉树)的更多相关文章
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
- 【leetcode】Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- 【LeetCode】103. Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
随机推荐
- 还在繁琐的敲MVP接口和实现类吗,教你一秒搞定。
只有程序员懒起来,才能提高开发效率 233333 在MVP的使用过程中,我们需要反复的去写各种MVP的接口和实现类, 实在是 太麻烦了!!所以抽时间撸了一款插件(只可用于Intellj IDEA 和 ...
- Android官方命令深入分析之hprof-conv
hprof-conv工具可以将Android SDK工具生成的HPROF文件生成一个标准的格式,这样你就可以使用工具进行查看: hprof-conv [-z] <infile> <o ...
- J2EE学习从菜鸟变大鸟之四 JNDI(Java Naming and Directory Interface)
掌握 J2EE 是件不是很轻松的事哈,但是很有意思,抽象抽象哈哈,因为它包含的技术和缩略语在不断地增长.Java 命名和目录接口(Java Naming and Directory Interface ...
- react-native版本升级
时刻将React Native更新到最新的版本,可以获得更多API.视图.开发者工具以及其他一些好东西(译注:官方开发任务繁重,人手紧缺,几乎不会对旧版本提供维护支持,所以即便更新可能带来一些兼容上的 ...
- SecureCRT设置linux终端显示颜色
在linux系统上,我们使用终端时,对于文件或目录会显示不同的颜色.而SecureCRT默认显示的颜色是单一的,我们该如何让其像linux一样显示个性化颜色呢. 使用SecureCRT登录 linux ...
- oracle中去重复记录 不用distinct
用distinct关键字只能过滤查询字段中所有记录相同的(记录集相同),而如果要指定一个字段却没有效果,另外distinct关键字会排序,效率很低 . select distinct name fro ...
- AngularJS进阶(三十五)浏览器兼容性解决之道
浏览器兼容性解决之道 前言 浏览器兼容性一直是前端开发中不得不面对的一个问题.而最突出的就是IE.对绝大多数公司来说,兼容IE6的性价比已经很低,而IE7则几乎已经绝迹.所以,常见的兼容性下限是IE8 ...
- 《java入门第一季》之面向对象(内部类到底在哪里?)
/* 内部类概述: 把类定义在其他类的内部,这个类就被称为内部类. 举例:在类A中定义了一个类B,类B就是内部类. 内部的访问特点: A:内部类可以直接访问外部类的成员,包括私有. B:外部类要访问内 ...
- 程序员的视角:java GC
GC(Garbage Collection 垃圾回收)的概念随着 java 的流行而被人们所熟知. 实际 GC 最早起源于20世纪60年代的 LISP 语言,是一种自动的内存管理机制. GC 要解决的 ...
- C/C++ Volatile关键词深度剖析(转)
本文转载自博文C/C++ Volatile关键词深度剖析. 背景 前几天,发了一条如下的微博 (关于C/C++ Volatile关键词的使用建议): 此微博,引发了朋友们的大量讨论:赞同者有之:批评者 ...