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

解题思路:

直接在上题加一条反转语句即可,JAVA实现如下:

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (root == null)
return list;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (queue.size() != 0) {
List<Integer> alist = new ArrayList<Integer>();
for (TreeNode child : queue)
alist.add(child.val);
if(list.size()%2!=0)
Collections.reverse(alist);
list.add(new ArrayList<Integer>(alist));
Queue<TreeNode> queue2 = queue;
queue = new LinkedList<TreeNode>();
for (TreeNode child : queue2) {
if (child.left != null)
queue.add(child.left);
if (child.right != null)
queue.add(child.right);
}
}
return list;
}

Java for LeetCode 103 Binary Tree Zigzag Level Order Traversal的更多相关文章

  1. leetcode 103 Binary Tree Zigzag Level Order Traversal ----- java

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  2. [LeetCode] 103. Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  3. [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树来回遍历

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  4. [LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

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

  6. [leetcode] 103 Binary Tree Zigzag Level Order Traversal (Medium)

    原题链接 题目要求以"Z"字型遍历二叉树,并存储在二维数组里. 利用BFS,对每一层进行遍历.对于每一层是从左还是从右,用一个整数型判断当前是偶数行还是奇数行就可以了. class ...

  7. Leetcode#103 Binary Tree Zigzag Level Order Traversal

    原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...

  8. [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历

    相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...

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

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

随机推荐

  1. softmax函数python实现

    import numpy as np def softmax(x): """ 对输入x的每一行计算softmax. 该函数对于输入是向量(将向量视为单独的行)或者矩阵(M ...

  2. 字符集研究之多字节字符集和unicode字符集

    作者:朱金灿 来源:http://blog.csdn.net/clever101 本文简介计算机中两大字符集:多字节字符集和unicode字符集的出现及关系. 首先我们须要明确的是计算机是怎样找到字符 ...

  3. C++必知必会(1)

    条款1数据抽象 抽象数据类型的用途在于将变成语言扩展到一个特定的问题领域.一般对抽象数据类型的定义须要准训下面步骤: 1.     为类型取一个描写叙述性的名字 2.     列出类型所能运行的操作 ...

  4. 关于郭天祥51开发板无法烧敲代码问题的解决(Prolific USB-to-Serial Comm Port)

    1. 事件背景: 因为使用了win8系统,之前购买的郭天祥C51开发板在通过一个两头都是usb口的下载线下载程序时出现了问题:下载工具stc isp无法连接到开发板上的串口,所以无法下载程序到c51开 ...

  5. Android---61---TabHost简单使用

    与TabHost结合使用的组件: TabWidget:代表选项卡的标签条 TabSpec:代表选项卡的一个Tab页面 TabHost不过一个简单的容器,它提供两个方法来创建.加入选项卡 newTabS ...

  6. UML的基本图(三)

     An artifact diagram shows the physical constituents of a system on the computer. Artifacts includ ...

  7. SAS学习经验总结分享:篇三—SAS函数

    SAS函数学习 文章为原创,禁止复制或转载,转载请标明出处, http://www.cnblogs.com/smallcrystal/p/4842346.html 1.函数输写格式: 1)一般书写格式 ...

  8. 四种常见的POST提交数据方式

    POST一般用来向服务端提交数据,有四种提交数据的格式,分别是: 1.application/x-www-form-urlencoded 2.application/json 3.multipart/ ...

  9. android xml布局文件中tools:layout的作用

    摘要 用最新版本的adt 创建一个基于master/detail flow 模版的app的时候,生成的 activity_item_list.xml 文件中有一个tools:layout属性: fra ...

  10. JAVA实现KNN分类

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/51064307 http://www.llwjy.com/blogdetail/f ...