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命令行基本命令操作
进入命令模式后, 显示所有数据库 show databases; 选定某个数据库 use 数据库名; 创建数据库 create database 数据库名; 删除数据库 drop table 数据库名 ...
- angularJS的路由!
angularJS 路由:(分发需求) angularJS 中路由是单独提供的功能模块,ngRoute 也是一个单独发行的文件 可以通过 npm 去安装这个包:angular-route <s ...
- JS生成GUID方法
function GUID() { this.date = new Date(); /* 判断是否初始化过,如果初始化过以下代码,则以下代码将不再执行,实际中只执行一次 */ if (typeof t ...
- binlog分析方法
线上处理问题的时候经常遇到业务说写入了但是数据不对,这种情况八成是业务改了之后又改过来了.但是我们需要证据,下面我们就说说简单的binlog分析 工具:mysqlbinlog #-vvv显示明文sql ...
- centos 搭建php环境
安装Apache1.安装yum -y install httpd2.开启apache服务systemctl start httpd.service3.设置apache服务开机启动systemctl e ...
- nginx中获取真实ip(转)
原文:http://blog.csdn.net/a936676463/article/details/8961504 server { listen 80; server_name lo ...
- yum whatprovides 查找哪个包可以提供缺失的文件
yum whatprovides 查找哪个包可以提供缺失的文件
- Mirror--生成用于镜像用户同步的脚本
USE master GO IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL DROP PROCEDURE sp_hexadecimal GO CREATE PR ...
- Hadoop MapReduce Task的进程模型与Spark Task的线程模型
Hadoop的MapReduce的Map Task和Reduce Task都是进程级别的:而Spark Task则是基于线程模型的. 多进程模型和多线程模型 所谓的多进程模型和多线程模型,指的是同一个 ...
- mysql索引之聚簇索引与非聚簇索引
1 数据结构及算法基础 1.1 索引的本质 官方定义:索引(Index)是帮助MySQL高效获取数据的数据结构 本质:索引是数据结构 查询是数据库的最主要功能之一.我们都希望查询速度能尽可能快,因此数 ...