Question

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,#,#,15,7},

    3
/ \
9 20
/ \
15 7

return its zigzag level order traversal as:

[
[3],
[20,9],
[15,7]
]

Solution

Traditional way is to use two queues to implement level order traversal. Here, we just add a flag to indicate whether it's from left to right or from right to left.

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (root == null)
return result;
// Set a flag to help judge traversal sequence
// flag = 0, from left to right; flag = 1, from right to left
int flag = 0;
List<TreeNode> current = new ArrayList<TreeNode>();
List<TreeNode> next;
current.add(root); while (current.size() > 0) {
List<Integer> oneRecord = new ArrayList<Integer>();
next = new ArrayList<TreeNode>();
for (TreeNode tmpNode : current) {
if (tmpNode.left != null)
next.add(tmpNode.left);
if (tmpNode.right != null)
next.add(tmpNode.right);
if (flag == 0)
oneRecord.add(tmpNode.val);
else
oneRecord.add(0, tmpNode.val);
}
result.add(oneRecord);
current = next;
flag = 1 - flag;
}
return result;
}
}

Binary Tree Zigzag Level Order Traversal 解答的更多相关文章

  1. 【leetcode】Binary Tree Zigzag Level Order Traversal

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. New Year Transportation(水)

    New Year Transportation Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. Linux远程登录

    Linux远程登录 远程登录 关闭linux的防火墙 /etc/init.d/iptables stop 启动VNC服务器 Vncserver & 然后记住desktop is localho ...

  3. CSS---------------之文本类型

    通过font来改变文本,主要从以下几个方面 字体加粗,字体的风格:斜体和字体变形:小型大写字母 字体的大小 行高 字体 示例如下 p{font:italic 75%/125% "Comic ...

  4. java final 和instanceof 关键字

    /* final class A { public final void eat(){ System.out.println("测试"); } } */ class A { } c ...

  5. 1.iOS第一个简单APP

    大纲: iOS系统发展 UI和OC 简单的APP程序 程序的生命周期   1.iOS的系统发展 从1983年OC程序开始发展到2015年,30多年的时间,但这依然不是一个十分完善的语言,可以说现在都没 ...

  6. hdu1161Eddy's mistakes

    Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for e ...

  7. .C .h 和 .CCP的区别

    1.*.H:C语言规定使用一个变量或调用一个函数前必须声明,为了使用方便,经常把常用函数,例如Windows API的函数,MFC类写入头文件.h,这样每次需要引用时只要使用#include加入就可以 ...

  8. POJ 2140 Herd Sums

    http://poj.org/problem?id=2140 Description The cows in farmer John's herd are numbered and branded w ...

  9. 求a,b在区间上的公倍数个数

    给你两个数 a,b.问你区间 [1,N]中都是有多少个数是a,b的公倍数.当数据很大的时候,遍历肯定会超时.其实,我们可以首先求出 lcm(a,b).因为我们知道(a,b)公倍数都是它最小公倍数的倍数 ...

  10. 【Solr专题之九】SolrJ教程

    一.SolrJ基础 1.相关资料 API:http://lucene.apache.org/solr/4_9_0/solr-solrj/ apache_solr_ref_guide_4.9.pdf:C ...