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 ...
随机推荐
- 07 总结ProgressDialog 异步任务
1,ProgressDialog > //使用对象 设置标题 progressDialog.setTitle("标题"); ...
- 学习TensorFlow,浅析MNIST的python代码
在github上,tensorflow的star是22798,caffe是10006,torch是4500,theano是3661.作为小码农的我,最近一直在学习tensorflow,主要使用pyth ...
- 03 ImageView 图片
四 ImageView 父类 : view >概念:展示图片的控件 >属性: <!-- android:adjustViewBounds=&qu ...
- java 单元测试教程(junit)
单元测试概念:最小化测试 比如说你想测试某个类中的一个方法 优点:无须启动整个程序 clipse使用junit教程: (一)配置jar: 1.右键工程选择Build Path 在二级菜单选择 Add ...
- 如何通过rsync+sersync 实现同步备份
3.rsync+sersync更快更节约资源实现web数据同步4.unison+inotify实现web数据双向同步 一:为什么要实现同步备份 服务器上有些重要文件或数据时,可以把他们多备份一份到其他 ...
- 我眼中的Linux设备树(二 节点)
二 节点(node)的表示首先说节点的表示方法,除了根节点只用一个斜杠"/"表示外,其他节点的表示形式如"node-name@unit-address".@前边 ...
- Device Tree Usage(理解DTS文件语法)
Basic Data Format The device tree is a simple tree structure of nodes and properties. Properties are ...
- 小试ImageMagik——使用篇
===================================================== ImageMagick的使用和开发的文章: 小试ImageMagik--使用篇 小试Imag ...
- iOS中 用FMDB封装一个SQLite数据库
建立一个单例: DataBaseHandle.h #import <Foundation/Foundation.h> @class PersonModel; @class FMDataba ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十三)
好了,现在在iOS模拟器中编译运行App,一切貌似都很好. 且慢,我们还没有到真机上调试呢?按说在编写App'时,无论如何应该尽快尽早在真机上调试.否则可能会碰到意想不到的问题,这次就是如此. 在真机 ...