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 right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
算法分析:和前面问题类似。
public class BinaryTreeZigzagLevelOrderTraversal
{
public List<List<Integer>> zigzagLevelOrder(TreeNode root)
{
List<Integer> list = new ArrayList<>();
List<List<Integer>> res = new ArrayList<>(); if(root == null)
{
return res;
} Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>(); s1.push(root);
while(!s1.isEmpty() || !s2.isEmpty())
{
while(!s1.isEmpty())
{
TreeNode temp = s1.pop();
if(temp.left != null)
{
s2.push(temp.left);
}
if(temp.right != null)
{
s2.push(temp.right);
}
list.add(temp.val);
}
if(list.size() != 0)
res.add(list);
list = new ArrayList<>();
while(!s2.isEmpty())
{
TreeNode temp = s2.pop();
if(temp.right != null)
{
s1.push(temp.right);
}
if(temp.left != null)
{
s1.push(temp.left);
}
list.add(temp.val);
}
if(list.size() != 0)
res.add(list);
list = new ArrayList<>();
}
return res;
}
}
Binary Tree Zigzag Level Order Traversal,z字形遍历二叉树,得到每层访问的节点值。的更多相关文章
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- 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】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 ...
- 剑指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】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
随机推荐
- mysql存储过程基础示例
转自:http://database.51cto.com/art/201608/516661.htm http://www.cnblogs.com/mark-chan/p/5384139.html D ...
- MVC认识
1.ASP.NET两种开发模式的简单比较(WebForm和MVC) (1)WebForm开发模式 当用户输入网址https://i.cnblogs.com/EditPosts.aspx?opt=1进行 ...
- ArcGIS for Server的安装及站点中的集群配置 分类: ArcGIS for server 2015-07-18 14:14 16人阅读 评论(0) 收藏
坚信并为之坚持是一切希望的原因. (不足之处,欢迎批评指正!) --------------------环境:Windows server2008R2虚拟机两台----------------- ...
- Http协议中Cookie详细介绍(转)
原文:http://www.169it.com/article/3217120921.html Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存C ...
- altium designer生成gerber步骤
什么是gerber文件 Gerber文件是所有电路设计软件都可以产生的文件,在电子组装行业又称为模版文件(stencil data),在PCB制造业又称为光绘文件.可以说Ger ...
- mysql数据库表的查询操作-总结
转自:https://www.cnblogs.com/whgk/p/6149009.html 序言 1.MySQL表操作(创建表,查询表结构,更改表字段等), 2.MySQL的数据类型(CHAR.VA ...
- Spring整合Mybatis 之分页插件使用
[分页插件项目中的正式代码一共有个5个Java文件,这5个文件的说明如下] Page<E>[必须]:分页参数类,该类继承ArrayList,虽然分页查询返回的结果实际类型是Page< ...
- Linux proc 内存
ps: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 4238 0 ...
- Python 安装pytz
1. https://pypi.org/project/pytz/#files 2. 下载上图标黄的文件, 3. pip install 4. from pytz import ...
- springboot+mybatis项目自动生成
springboot_data_access_demo基于rapid,根据自定义模版生成的基于mybatis+mysql的数据库访问示例项目.简单配置数据库信息,配置不同的生成策略生成可以直接运行访问 ...