【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal
[LeetCode]
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
Total Accepted: 44275 Total Submissions: 165753 Difficulty: Medium
Question
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).
Examples
For example:
Given binary tree {3,9,20,#,#,15,7},3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[3],
[20,9],
[15,7]
]
Ways
方法一
使用队列的方法。
学习了java队列的使用。在一个队列中,放入第一个元素,取出后,放入左右孩子,再根据左右孩子逐个放入其后相应的孩子。队列为空的时候,说明已经遍历完成。
这个方法非常好,学习了。
加入标志符,根据标识符 来判断本层是否倒转
倒序算法:
Collections.reverse(result);
方法二
使用栈的方法。
使用两个栈,一个栈放本层的节点,另外一个栈放下一层的栈。节点值加入List之后,将两个栈互换 。这个方法中要根据本层是否要倒序,判断是否颠倒加入栈中。
栈的主要方法:
currLevel.push(root);
TreeNode node = currLevel.pop();
Solution
托管在我的GitHub上:
https://github.com/fuxuemingzhu/ZigzagLevelOrder
Captures
测试结果截图:

Reference
http://www.jiuzhang.com/solutions/binary-tree-zigzag-level-order-traversal/
Date
2015/10/14 23:34:11
【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告的更多相关文章
- [leetcode]Binary Tree Zigzag Level Order Traversal @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tr ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [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解题报告—— 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 Given a binary tree, return the zigzag level order traversa ...
随机推荐
- R语言与医学统计图形-【10】ggplot2图形映射
ggplot2绘图系统--图形映射 颜色的映射. #aes中映射变量 ggplot()+geom_point(aes(x=carat,y=price,color='blue'),#color视为单一变 ...
- Python中的随机采样和概率分布(二)
在上一篇博文<Python中的随机采样和概率分布(一)>(链接:https://www.cnblogs.com/orion-orion/p/15647408.html)中,我们介绍了Pyt ...
- day13 装饰器与语法糖
day13 装饰器与语法糖 一.装饰器 1.什么是装饰器 装饰器就是装饰别人的工具,具体是指为被装饰者添加新功能 装饰器->函数 被装饰者->函数 2.为何要用装饰器 装饰器的核心思想:( ...
- MediaPlayer详解
[1]MediaPlayer 详细使用细则 [2]MediaPlayer使用详解_为新手准备 [3]MediaPlayer 概览
- spring注解-自动装配
Spring利用依赖注入(DI)完成对IOC容器中中各个组件的依赖关系赋值 一.@Autowired 默认优先按照类型去容器中找对应的组件(applicationContext.getBean(Boo ...
- Java Spring 自定义事件监听
ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...
- When do we pass arguments by reference or pointer?
在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...
- SVN终端演练(个人开发\多人开发)
SVN终端演练(个人开发) ### 1. 命令格式 命令行格式: svn <subcommand> [options] [args] svn 子命令 [选项] [参数] ...
- 【Linux】【Basis】网络
Linux网络属性配置 计算机网络: TCP/IP:协议栈(使用) ISO,OSI:协议栈(学习) ...
- 【Matlab】线性调频信号LFM 仿真
[知识点] 生成序列 i = a:step:b 举例: i = 1:1:9 画图(子图) subplot(m,n,p)或者subplot(m n p) 总结起来就是,画一个m行n列的图. p表示在第p ...