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. Jsp2.0自定义标签(第二天)——自定义循环标签

    今天是学习自定义标签的第二天,主要是写一个自定义的循环标签. 先看效果图: 前台页面Jsp代码 <%@ page language="java" contentType=&q ...

  2. cocos3.x新建项目

    cocos new -p FishRunMan com.game.simple -l cpp

  3. dedecms调用新闻文章列表

    效果如下: 代码如下: <div class="list"> <ul class="d6 ico4"> {dede:list pages ...

  4. HDU1087上升子序列的最大和

     解法一 此题是一个简单的动态规划问题,用dp[i]记做最后一步经过第i个数所得到的最大sum值,则结果=max(dp[i]),i=1,...n.考虑dp[i]的前一步会经过那里?假设dp[i]的前一 ...

  5. Linux下监听或绑定(bind)21端口失败

    问题:写了一个程序,尝试在21端口监听,结果在执行bind的时候失败了. sockaddr_in sock_addr; sock_addr.sin_family = AF_INET; sock_add ...

  6. git错误解决 -- 小结

    1.今天 当我  执行  Git add  somefile 的时候,出现 如下 错误: If no other git process is currently running, this prob ...

  7. 线程安全的概念和Synchronized(读书笔记)

         并行程序开发的一大关注重点就是线程安全,一般来说,程序并行化为了获取更多的执行效率,但前提是,高效率不能以牺牲正确性为代价,线程安全就是并行程序的根本和根基.volatile并不能真正保证线 ...

  8. 理解支持向量机(三)SMO算法

    在支持向量机模型的求解中,我们用到了SMO算法来求解向量α. 那么什么是SMO算法?在讲SMO算法之前.我们须要先了解下面坐标上升法. 1.坐标上升法 如果有优化问题: W是α向量的函数.利用坐标上升 ...

  9. 苹果证书的申请、unityoc交互基础

    注冊开发人员账号时:注意不要使用中国邮箱 99美金证书 :仅仅支持上传AppStore. 299美金证书:指的的我开发的应用是仅仅支持打包安装.企业级的. 假设申请了开发人员账号.也就是交了那几百美金 ...

  10. HDFS源码分析之DataXceiverServer

    DataXceiverServer是Hadoop分布式文件系统HDFS的从节点--数据节点DataNode上的一个后台工作线程,它类似于一个小型的服务器,被用来接收数据读写请求,并为每个请求创建一个工 ...