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 ...
随机推荐
- highmaps如何自定义 区间的颜色刻度
https://api.highcharts.com/highmaps/colorAxis.dataClassColor http://jsfiddle.net/gh/get/library/pure ...
- Mac - iPhone屏幕录制
1. Mac电脑屏幕录制 1.1 文件->新建屏幕录制 1.2 点击红色按钮 1.3 截取需要录制的屏幕部分,点击开始录制 1.4 点击工具栏的停止按钮,停止录制 1.5 然后会 ...
- Scala面向对象和模式匹配
我们要封装数据,定义模板等操作,所以我们需要面向对象. 一.scala中的单例对象 在scala当中,是没有static的,scala给我们提供了单例模式的实现方法.就是使用关键字object. st ...
- 【css a标签 鼠标悬浮时变手型】
<a href="#" style="cursor:pointer">
- Python开发【模块】:torndb
Torndb模块 概要:torndb是一个轻量级的基于MySQLdb封装的一个模块,其是tornado框架的一部分.其项目主页为:https://github.com/bdarnell/torndb ...
- neovim 使用
neovim配置与vim兼容,配置文件在~/.config/nvim 终端 :terminal 进入shell模式 <C-\><C-n> 退出终端 <M-i> 重新 ...
- apache ab test使用 单独安装ab和htpasswd
apache ab test使用 apache ab test使用 单独安装ab和htpasswd 转载自: http://www.cnblogs.com/super-d2/p/3831155.htm ...
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求 The serverRuntime@appConcurrentRequestLimit setting is being exceeded.
今天下午17点左右,博客园博客站点出现这样的错误信息: Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@a ...
- PAT 1073 Scientific Notation[字符串处理][科学记数法]
1073 Scientific Notation(20 分) Scientific notation is the way that scientists easily handle very lar ...
- 使用jQuery包装节点
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...