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 ...
随机推荐
- [cacti]nginx+php+cacti+mysql+php-fpm 安装小记
网上教程很多,但是nginx不太多,下面安装时候主要参考的篇文章: http://54im.com/linux/linux-cacti-cn-install.html http://www.tecmi ...
- 饮一盏Bug留香,唱一曲项目飞扬
沿途的风景 牵挂的项目 两情迢迢 学生档案管理项目在2月的末尾从稍带寒意的季节里完成了第一次迭代,验收的结果不尽善尽美,演示的功能也惨不忍睹,各种"关爱"的点评充斥耳旁 ...
- 03 EditText文本编辑框
二 EditText 文本编辑框 父类: TextView >概念:文本编辑框 可以进行文本编辑 android:textColor="#00f&qu ...
- iOS开发之一:入门介绍
今天就介绍一下iOS开发的基本的东西,有很多东西都是经常用到的而我却经常记不住,所以还是写下来吧. iOS开发需要的开发工具是Xcode,而Xcode又必须运行在 OS X(苹果系统)环境下,所以我们 ...
- Intent的Data和Type和Flag属性-amdroid学习之旅(五十一)
Data属性介绍 使用Data和Action属性启动系统Activity 代码示例 public class MainActivity extends Activity{ @Override prot ...
- JSP连接MySQL时老是遇到驱动错误怎么办?
在使用JSP进行web开发的时候总是会不可避免的遇到各种各样的问题.今天我也来讲一讲我遇到的一些奇葩的问题. 驱动出错 一开始我总是以为是我导入到工程的里的jar包的问题,于是我就试验了好几个连接My ...
- JavaScript 关键字
JavaScript 关键字 和其他任何编程语言一样,JavaScript 保留了一些关键字为自己所用. JavaScript 同样保留了一些关键字,这些关键字在当前的语言版本中并没有使用,但在以后 ...
- hive的map类型处理
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-CollectionFunc ...
- C#调用GDAL算法进度信息传递
GDAL库中提供了很多的算法,同时也提供了进度条的参数.对于C++调用来说,应该没什么问题,但是对C#调用来说,在进度条这块需要写一个代理来进行传递.首先写一个简单的测试代码. 首先定义一个委托函数原 ...
- 从JDK源码角度看java并发的原子性如何保证
JDK源码中,在研究AQS框架时,会发现很多地方都使用了CAS操作,在并发实现中CAS操作必须具备原子性,而且是硬件级别的原子性,java被隔离在硬件之上,明显力不从心,这时为了能直接操作操作系统层面 ...