LeetCode103 BinaryTreeZigzagLevelOrderTraversal(二叉树Z形层次遍历) Java题解
题目:
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题解的更多相关文章
- [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 ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
- [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 ...
- 32-3题:LeetCode103. Binary Tree Zigzag Level Order Traversal锯齿形层次遍历/之字形打印二叉树
题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...
- LeetCode103. 二叉树的锯齿形层次遍历
103. 二叉树的锯齿形层次遍历 描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 示例 例如,给定二叉树: [3,9,2 ...
- Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / ...
- Java实现 LeetCode 103 二叉树的锯齿形层次遍历
103. 二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7], 3 ...
随机推荐
- 全栈JavaScript之路(十三)了解 ElementTraversal 规范
支持Element Traversal 规范的浏览器有IE 9+.Firefox 3.5+.Safari 4+.Chrome 和Opera 10+. 对于元素间的空格,在IE9之前.都不会返回文档节点 ...
- MySQL-SQL语句中SELECT语句的执行顺序
SELECT语句的执行顺序大家比较少关注,下面将为您详细介绍SQL语句中SELECT语句的执行顺序,供您参考,希望对您能够有所帮助. SELECT语句的执行的逻辑查询处理步骤: (8)SELECT ( ...
- 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- evernote如何笔记共享
首先将你的笔记共享 访问你的共享笔记地址,就可看到共享笔记的内容了 https://www.evernote.com/pub/用户名/笔记名 https://www.evernote.com/ ...
- Debian/Ubuntu架设nginx+bugzilla
题注 最近需要一个简单快速的bug追踪工具,考虑到系统环境以及学习成本,决定采用bugzilla.不试不知道,原来这里面还有这么多的坑需要一个个踩平~,遂随笔一下以备后用. 我采用的系统组成是debi ...
- map以自定义类型当Key
关于map的定义: template < class Key, class T, class Compare = less<Key>, class Allocator = alloc ...
- Python 列表 pop() 方法
描述 Python 列表 pop() 方法通过指定元素的索引值来移除列表中的某个元素(默认是最后一个元素),并且返回该元素的值,如果列表为空或者索引值超出范围会报一个异常. 语法 pop() 方法语法 ...
- debian更新源时找不到公钥的解决办法
W: GPG error: http://mirrors.163.com jessie-updates InRelease: The following signatures couldn't be ...
- Sql 列转行 三种方法对比
合并列值 --******************************************************************************************* ...
- 统一日志监控系统 springboot websocket 简单版 王代军-作品
http://git.oschina.net/redArmy/springboot-websocket-logs 目的: 统一监控 开发测试环境日志 如果需要可以拓展线上环境的日志(自己视情况而定) ...