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],

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its zigzag level order traversal as:

  1. [
  2. [3],
  3. [20,9],
  4. [15,7]
  5. ]

这题仔细分析:按照之前层序遍历,是使用一个队列,但是这里使用队列不太好解决。
队列不行,就考虑栈,一个栈也不行,因为是深度遍历的。
这里使用两个栈,一个栈放奇数层,一个栈放偶数层。遍历第一个栈,它的输出是从左往右,依次将它的左右节点放到第二个栈中,接下来遍历第二个栈,输出就是从右往左了,然后将其右左节点依次放到第一个栈中,这样再遍历第一个栈时,输出就是从左往右了。。。

代码如下:注意其中的一个细节

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
  12. List<List<Integer>> res=new ArrayList<List<Integer>>();
  13. if(root==null) return res;
  14. Stack<TreeNode> s1=new Stack<>();
  15. Stack<TreeNode> s2=new Stack<>();
  16. List<Integer> list=new ArrayList<>();
  17. s1.push(root);
  18. while(!s1.isEmpty()||!s2.isEmpty()){
  19. if(!s1.isEmpty()){
  20. int size=s1.size();
  21. for(int i=0;i<size;i++){
  22. TreeNode node=s1.pop();
  23. list.add(node.val);
  24. if(node.left!=null) s2.push(node.left);
  25. if(node.right!=null) s2.push(node.right);
  26. }
  27. res.add(list);
  28. list=new ArrayList<>();
  29. }
  30. if(!s2.isEmpty()){
  31. int size=s2.size();
  32. for(int i=0;i<size;i++){
  33. TreeNode node=s2.pop();
  34. list.add(node.val);
  35. if(node.right!=null) s1.push(node.right);
  36. if(node.left!=null) s1.push(node.left);
  37. }
  38. res.add(list);
  39. list=new ArrayList<>();
  40. }
  41. }
  42. return res;
  43. }
  44. }

Binary Tree Zigzag Level Order Traversal(z字形打印二叉树)的更多相关文章

  1. Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树

    题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...

  2. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  3. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

  4. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  5. 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 ...

  6. 剑指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 ...

  7. 【LeetCode】103. Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  8. [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 ...

  9. 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 ...

  10. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

随机推荐

  1. 07 总结ProgressDialog 异步任务

    1,ProgressDialog     >        //使用对象  设置标题             progressDialog.setTitle("标题");   ...

  2. 学习TensorFlow,浅析MNIST的python代码

    在github上,tensorflow的star是22798,caffe是10006,torch是4500,theano是3661.作为小码农的我,最近一直在学习tensorflow,主要使用pyth ...

  3. 03 ImageView 图片

    四  ImageView   父类 : view     >概念:展示图片的控件       >属性:      <!--  android:adjustViewBounds=&qu ...

  4. java 单元测试教程(junit)

    单元测试概念:最小化测试  比如说你想测试某个类中的一个方法 优点:无须启动整个程序 clipse使用junit教程: (一)配置jar: 1.右键工程选择Build Path 在二级菜单选择 Add ...

  5. 如何通过rsync+sersync 实现同步备份

    3.rsync+sersync更快更节约资源实现web数据同步4.unison+inotify实现web数据双向同步 一:为什么要实现同步备份 服务器上有些重要文件或数据时,可以把他们多备份一份到其他 ...

  6. 我眼中的Linux设备树(二 节点)

    二 节点(node)的表示首先说节点的表示方法,除了根节点只用一个斜杠"/"表示外,其他节点的表示形式如"node-name@unit-address".@前边 ...

  7. Device Tree Usage(理解DTS文件语法)

    Basic Data Format The device tree is a simple tree structure of nodes and properties. Properties are ...

  8. 小试ImageMagik——使用篇

    ===================================================== ImageMagick的使用和开发的文章: 小试ImageMagik--使用篇 小试Imag ...

  9. iOS中 用FMDB封装一个SQLite数据库

    建立一个单例: DataBaseHandle.h #import <Foundation/Foundation.h> @class PersonModel; @class FMDataba ...

  10. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十三)

    好了,现在在iOS模拟器中编译运行App,一切貌似都很好. 且慢,我们还没有到真机上调试呢?按说在编写App'时,无论如何应该尽快尽早在真机上调试.否则可能会碰到意想不到的问题,这次就是如此. 在真机 ...