题目:

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

解题:

这题事实上和二叉树的层序遍历非常类似  不过在这个基础上加上一个左右方向  一開始我的思维被局限住了  写了一个非常冗余的代码版本号  实现思路是这种 当须要从右到左输出的时候  我把队列里面的节点输出然后 反序存回队列

代码:

public static List<List<Integer>> zigzagLevelOrder(TreeNode root) {

		List<List<Integer>> result=new ArrayList<List<Integer>>();//存放终于结果
boolean isLeftToRight=false;//从左到右的方向
Queue<TreeNode> nodeQueue=new LinkedList<>();//存放节点 便于每一层遍历 //处理根节点
if(root==null)
return result;
else {
List<Integer> list=new ArrayList<>();
list.add(root.val);
result.add(list);
nodeQueue.offer(root);
} while(!nodeQueue.isEmpty())
{
int size=nodeQueue.size();
List<Integer> tempResult=new ArrayList<>();//用来临时存放每一层节点的遍历结果 Stack<TreeNode> stack=new Stack<>();//用来辅助将队列倒序 if(isLeftToRight)
{
//将队列里面的节点都先出队列进栈再出栈进队列 使得和原来的顺序刚好相反
for(int i=0;i<size;i++)
{
stack.push(nodeQueue.poll());
}
while(!stack.isEmpty())
nodeQueue.offer(stack.pop()); while(size>0)//从左到右
{
size--;
TreeNode tempNode=nodeQueue.poll();
if(tempNode.left!=null)
{
nodeQueue.offer(tempNode.left);
tempResult.add(tempNode.left.val);
}
if(tempNode.right!=null)
{
nodeQueue.offer(tempNode.right);
tempResult.add(tempNode.right.val);
}
} if(!tempResult.isEmpty()) result.add(tempResult);
//循环退出 表示一层已经遍历完了 这时候重置方向标志位
isLeftToRight=false; }
else {
//将队列里面的节点都先出队列进栈再出栈进队列 使得和原来的顺序刚好相反
for(int i=0;i<size;i++)
{
stack.push(nodeQueue.poll());
}
while(!stack.isEmpty())
nodeQueue.offer(stack.pop()); while(size>0)//从右到左
{
size--;
TreeNode tempNode=nodeQueue.poll();
if(tempNode.right!=null)
{
nodeQueue.offer(tempNode.right);
tempResult.add(tempNode.right.val);
}
if(tempNode.left!=null)
{
nodeQueue.offer(tempNode.left);
tempResult.add(tempNode.left.val);
}
}
if(!tempResult.isEmpty()) result.add(tempResult);
//循环退出 表示一层已经遍历完了 这时候重置方向标志位
isLeftToRight=true;
}
} return result; }

后面看别人的解答,事实上全然没有必要在队列中反序  仅仅要在存每一层输出结果的ArrayList中反序存入就能够了  以下是这样的思路的代码:

public static List<List<Integer>> zigzagLevelOrder2(TreeNode root) {

		List<List<Integer>>  result=new ArrayList<List<Integer>>();//存放终于结果
Queue<TreeNode> nodeQueue=new LinkedList<>();//存放节点
int flag=1;//flag为奇数的时候 从左到右 为偶数的时候从右到左。 if(root==null)
return result;
else {
nodeQueue.add(root);
} while(!nodeQueue.isEmpty())
{
int count=nodeQueue.size();
List<Integer> tempResult=new ArrayList<>();
while(count>0)
{
TreeNode tempNode=nodeQueue.poll();
count--;
if(flag%2!=0)//从左到右
{
tempResult.add(tempNode.val);
}
else {//
tempResult.add(0,tempNode.val);
} if(tempNode.left!=null)
nodeQueue.add(tempNode.left);
if(tempNode.right!=null)
nodeQueue.add(tempNode.right);
}
if(!tempResult.isEmpty()) result.add(tempResult);
flag++;
} return result; } }

看代码的长度就知道我之前的有多复杂了

LeetCode103 BinaryTreeZigzagLevelOrderTraversal(二叉树Z形层次遍历) Java题解的更多相关文章

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

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

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

  3. [Swift]LeetCode103. 二叉树的锯齿形层次遍历 | 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. 32-3题:LeetCode103. Binary Tree Zigzag Level Order Traversal锯齿形层次遍历/之字形打印二叉树

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

  5. LeetCode103. 二叉树的锯齿形层次遍历

    103. 二叉树的锯齿形层次遍历 描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 示例 例如,给定二叉树: [3,9,2 ...

  6. Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历

    给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / ...

  7. Java实现 LeetCode 103 二叉树的锯齿形层次遍历

    103. 二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null ...

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

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

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

    给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7],    3   ...

随机推荐

  1. 微信小程序调用后台接口+热点新闻滚动展示

    1.微信JS文件,发送请求调用:  //将返回接口数据,写入Page({data})里面 //获取热点新闻,这个也是写在onload:function(){//code)里面的 wx.request( ...

  2. Python学习笔记011——内置函数eval()

    1 描述 eval()  函数用来执行一个字符串表达式,并返回表达式的值 2 语法 原文 eval(expression[, globals=None[, locals=None]]) express ...

  3. Android中asset文件夹和raw文件夹区别(转载)

    原文地址:http://www.cnblogs.com/leizhenzi/archive/2011/10/18/2216428.html *res/raw和assets的相同点: 1.两者目录下的文 ...

  4. A class file was not written. The project may be inconsistent, if so try refreshing this project and building it. eclipse提示错误

    感觉很奇怪,查看了一下磁盘,发现workspace所在磁盘已经满了,删除一些文件之后,选择项目->Project->Clean...->选择Clean all projects-&g ...

  5. Linux中断 - tasklet

    一.前言 对于中断处理而言,linux将其分成了两个部分,一个叫做中断handler(top half),属于不那么紧急需要处理的事情被推迟执行,我们称之deferable task,或者叫做bott ...

  6. DataGridView在Cell编辑状态响应回车键下的KeyPress/KeyDown/KeyUp事件

    我们知道由于DataGridView的单元格DataGridCell处于编辑的时候,当你按Enter键,那么DataGridView是不会激发KewPress/KeyDown/KeyUp这些事件的,因 ...

  7. C语言中续行符“\”说明

    把一个预处理指示写成多行要用“\”续行,因为根据定义,一条预处理指示只能由一个逻辑代码行组成. 而把C代码写成多行则不必使用续行符,因为换行在C代码中只不过是一种空白字符,在做语法解析时所有空白字符都 ...

  8. &lt;二&gt;读&lt;&lt;大话设计模式&gt;&gt;之策略模式

    又和大家见面了.可以坚持写出第二篇文章真不错,好好加油. <<大话设计模式>>解说策略模式是以商场收银软件程序开头的,那么问题来了.哪家商场收银软件强,开玩笑了. 读过上篇文章 ...

  9. 如何学习Linux

    为啥我们要学习Linux 技术的价值不在于这个技术有多么高超,而在于技术本身给我们带来什么价值,所以很多时候我们学习一个技术,不能盲目学,是为了使用这个技术,知道这个技术的使用场景,知道这个技术带来的 ...

  10. TinyXml 操作XML 常用操作

    源代码下载:http://sourceforge.net/projects/tinyxml/files/ 如下是一个XML片段:    <Persons>        <Perso ...