真是不容易啊,做这道题的时候脑子一团乱,感觉还是得劳逸结合啊。这道题的思想不难,就是宽搜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. Java集合框架(1)

    Collection接口:它是Java集合框架的一个根接口,也是List.Set和Queue接口的父接口.同时它定义了可用于操作List.Set和Queue的方法—增删改查. Map接口:它提供了一种 ...

  2. Nginx的一些优化(突破十万并发)

    Nginx的一些优化(突破十万并发) nginx指令中的优化(配置文件) worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数. worker_cpu_a ...

  3. 《精通Spring4.X企业应用开发实战》读后感第四章(BeanFactory和ApllicationContext)

  4. Linux 之添加系统环境变量

    PATH 值是一系列目录,当执行命令时,linux就在这些目录下查找,其格式为: PATH=$PATH:<PATH1>:<PATH2>:<PATH3>:------ ...

  5. jzoj6002. 【PKUWC2019模拟2019.1.15】Permutation (组合数)

    题面 题解 设\(lim=(n-1)/2\)(这里是下取整),那么\(x\)位置的值最大不能超过\(lim\),而\(y\)处的值不能小于\(y\),于是有\[Ans=\sum_{i=1}^{lim} ...

  6. SAP屏幕字段常用代码集合

    SAP屏幕字段常用代码集合 ().Screen 设计 TABLES: SSCRFIELDS. PARAMETERS: P_EBLEN LIKE VBRK-EBLEN DEFAULT ' '. PARA ...

  7. hyperledger fabric 1.0.5 分布式部署 (八)

    gdb debug peer 程序 在开始我们从 github 上download 下来的源码包,实际上已经包含了可执行的 peer 程序,但是该程序是使用 release 方式编译的,并不支持gdb ...

  8. 你必须学写 Python 装饰器的五个理由

    你必须学写Python装饰器的五个理由 ----装饰器能对你所写的代码产生极大的正面作用 作者:Aaron Maxwell,2016年5月5日 Python装饰器是很容易使用的.任何一个会写Pytho ...

  9. react native 使用iconfont

    安卓的配置: 1.在android/app/src/assets/fonts文件夹下复制从iconfont下载的   (下载后的icon名称可以在下载来的文件中的html文件中) 2.在android ...

  10. JDK 简介

    JDK简介 JDK java开发工具包 JRE java 运行时环境 JVM java虚拟机 三者的关系:JDK 包含 JRE,JRE 包含 JVM Java的核心优势是跨平台,由JVM虚拟机实现的. ...