真是不容易啊,做这道题的时候脑子一团乱,感觉还是得劳逸结合啊。这道题的思想不难,就是宽搜BFS。通过设置一个flag来判断是否需要逆序输出。

我的做法虽然AC,但是觉得代码还是不好,空间占用较多。

 /**
* 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< >();
Queue<TreeNode> queue = new LinkedList<>(); if(root == null)
{
return result;
}
queue.add(root);
boolean flag = true;
while (!queue.isEmpty())
{
int i=0;
List<Integer> tempList = new ArrayList<>();
int count = queue.size();
while (i<count) {
TreeNode Node = queue.poll();
tempList.add(Node.val);
if (Node.left != null) {
queue.add(Node.left);
}
if (Node.right != null) {
queue.add(Node.right);
}
i++;
}
if(flag==false)
{
Stack<Integer> tempstack = new Stack<>();
for (Integer tem: tempList
) {
tempstack.push(tem);
}
List<Integer> list2 = new ArrayList<>();
while (!tempstack.isEmpty())
{
list2.add(tempstack.pop());
}
result.add(list2); }
else
{
result.add(tempList);
}
flag=!flag;
}
return result;
}
}

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

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

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

  2. [leetcode]Binary Tree Zigzag Level Order Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tr ...

  3. [LeetCode] 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 :: Binary Tree Zigzag Level Order Traversal [tree, BFS]

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

  5. [Leetcode] 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 OJ--Binary Tree Zigzag Level Order Traversal *

    https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 树的层序遍历 使用两个 stack 或者 vect ...

  7. LeetCode: 103_Binary Tree Zigzag Level Order Traversal | 二叉树Zigzag层次遍历 | Medium

    本题也属于层次遍历的变形,不同之处在于其遍历的方法是交替进行的,形成一个ZigZag的曲线形式,如下: 代码如下: struct TreeNode { int val; TreeNode* left; ...

  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. selenium+python中,框架中,怎么返回上一个菜单

    /退回上一级表单 driver.switchTo().defaultContent();

  2. TCP 连接的握手信息详解

    建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...

  3. Ubuntu&nbsp;12.04&nbsp;Eclipse设…

    Ubuntu 12.04 Eclipse设置(黑色背景解决) 分类: ubuntu2012-11-21 10:47 252人阅读 评论(0) 收藏 举报 eclipseEclipseubuntuUbu ...

  4. 《Java多线程编程核心技术》读后感(六)

    多线程的死锁 package Second; public class DealThread implements Runnable { public String username; public ...

  5. 25.ProfileService实现(调试)

    上一节课拿到的AccessToken和IdToken 实现ProfileService类 在服务端 添加ProfileService类 需要继承IProfileServiuce 用到的画图工具 Ipr ...

  6. UVa 11100 The Trip, 2007 (题意+贪心)

    题意:有n个包,其中小包可以装到大的包里,包的大小用数字进行表示,求最小的装包数量. 析:这个题的题意不太好理解,主要是有一句话难懂,意思是让每个最大包里的小包数量的最大值尽量小,所以我们就不能随便输 ...

  7. Celery 基本使用

    1. 认识 Celery Celery 是一个 基于 Python 开发的分布式异步消息任务队列,可以实现任务异步处理,制定定时任务等. 异步消息队列:执行异步任务时,会返回一个任务 ID 给你,过一 ...

  8. 虚拟机vmware11装Mac ox 10.8 解决windows8.1下unlocker202 vmxsmc.exe已停止工作的问题

    转载:http://tieba.baidu.com/p/3485956366 先说说故事吧,相信很多图钉都用过VMware Workstation这个软件.我系统是win8.1up1,之前用VMwar ...

  9. 【转】insert忽略重复、mysql插入操作跳过、插入覆盖覆盖、mysql更新重复

    需求背景:一般情况,插入数据的时候,有脏数据的情况,主键重复的话,直接insert into 会报错的,然后下面的sql都不再执行了,如果可以确定后面的数据可以覆盖前面的数据,直接用replace i ...

  10. [Xcode 实际操作]七、文件与数据-(1)获取程序沙箱结构中常用的几个目录

    目录:[Swift]Xcode实际操作 本文将演示如何获取程序沙箱结构中,常见的几个目录. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit ...