Binary Tree Zigzag Level Order Traversal
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]
] 这道题主要用BFS解决,在BFS得到结果后需要考虑是否要逆序一下。逆序可以考虑用栈,我没有用栈,直接逆序
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>(); //保存最后的结果
if(null == root)
return result;
boolean needReverse = false; //是否要对列表中的内容进行逆序
Queue<TreeNode> queue = new LinkedList<TreeNode>(); //BFS要使用到的队列
queue.add(root); //根节点入队
while(!queue.isEmpty()){ //队列不为空
List<Integer> oneLevel = new ArrayList<Integer>(); //保存在同一层及节点的值
Queue<TreeNode> temp = new LinkedList<TreeNode>(); //存放下一层的节点
while(!queue.isEmpty()){
TreeNode headOfQueue = queue.remove(); //对头元素出队
oneLevel.add(headOfQueue.val);
if(headOfQueue.left != null)
temp.add(headOfQueue.left); //左子树不为空,入队
if(headOfQueue.right != null)
temp.add(headOfQueue.right); //右子树不为空,入队
}//while
if(needReverse)
reverseList(oneLevel);
result.add(oneLevel);
needReverse = !needReverse;
queue = temp;
}
return result;
} /**
* list列表中内容逆序
* @param list
*/
private void reverseList(List<Integer> list){
for(int i = 0, j = list.size() - 1; i < j; i++, j--){
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
Binary Tree Zigzag Level Order Traversal的更多相关文章
- 【leetcode】Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- 【LeetCode】103. Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
随机推荐
- Glassfish数据源配置详解
本文环境: Win2003 + myeclipse6.01 + sqlserver2000(sp4) 1. 安装glassfish——启动,在管理控制台下配置如下 首先配置连接池——大家把sqlser ...
- Backup App's data without rooting the phone
First I'd like to let you know that my phone is Android 6.0 Marshmallow. So it works on the latest A ...
- IntelliJ IDEA 快捷键和设置
IntelliJ IDEA 使用总结 http://my.oschina.net/xianggao/blog/97539 IntelliJ IDEA 问题解决:1.乱码,主要是快捷键的字样显示乱码 中 ...
- 控制不能离开Finally子句主体
1.在try{}catch{}finally{}的结构中不可以将返回语句放置在finally的主体当中2.如果在catch{}中有向上一级从新抛出异常操作,在finally{}之后的语句将不会执行 3 ...
- Silverlight读取Zip文件中的图片与视频
首先看看Demo的截图: 下面我将一步步展示实现这个Demo的过程,这个需求就是读出Zip文件中的图片与视频. Demo整体架构: 首先我们准备几张图片和视频,然后将其压缩至resource.zip文 ...
- Silverlight中动画的性能浅析
Silverlight中提供了StoryBoard实现动画,可是StoryBoard的性能实在不敢恭维,特别是动画很大的时候,计算机的CPU和内存的狂增,如此一来性能实在太差,在默认的动画效果中动画实 ...
- GemFire 入门篇1:GemFire 是什么?
一.GemFire是什么? 如果你了解Redis或memCached,那么恭喜,你很快就能理解GemFire是什么,没错,你可以把它理解为一个增强版的Redis,具体在哪些方面增强,我们日后慢慢聊 ...
- hive中的全排序
写mapreduce程序时,如果reduce个数>1,想要实现全排序需要控制好map的输出 现在学了Hive,写sql大家都很熟悉,如果一个order by解决了全排序还用那么麻烦写mapred ...
- 老外写的在桌面添加快捷方式(DELPHI XE5 ANDROID)
UsesAndroidapi.JNI.GraphicsContentViewText, FMX.Helpers.Android,Androidapi.JNI.JavaTypes, FMX.Platfo ...
- C# 平时碰见的问题【4】
1. 模糊查询 like的参数化写法 string keyword="value"; // 要模糊匹配的值 错误示范: sql: string strSql=" ...