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 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]
]
题目的意思非常直白。层序遍历整个树,可是第一层正序输出。第二层反序输出,第三层正序输出,以此类推。做法有两种:一、仍然採用level-travel,仅仅是引入一个标记,推断是否反转得到的数列; 二、考虑到stack的特点,利用stack FILO的特点来直接输出。两种方法都贴出来
利用stack的:
class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
bool isRe = false;
vector<int> a;
stack<TreeNode *> s1, s2;
if (root == NULL)
return ret;
s1.push(root);
while (!s1.empty()){
TreeNode *tmp = s1.top();
s1.pop();
a.push_back(tmp->val);
if (isRe){
if (tmp->right)
s2.push(tmp->right);
if (tmp->left)
s2.push(tmp->left);
}
else{
if (tmp->left)
s2.push(tmp->left);
if (tmp->right)
s2.push(tmp->right);
}
if (s1.empty()){
ret.push_back(a);
isRe = !isRe;
swap(s1, s2);
a.clear();
}
}
return ret;
}
private:
vector<vector<int>> ret;
};
利用queue的,这里因为引入了swap,所以能够复用同一个代码流程,代码会短一些;
class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int>> ret;
queue<TreeNode *> current, next; //利用两个队列的交替来区分每一层
bool isRe = false;
vector<int> v;
if (root == NULL)
return ret;
current.push(root);
while (!current.empty()){
TreeNode *tmp = current.front();
current.pop();
v.push_back(tmp->val);
if (tmp->left)
next.push(tmp->left);
if (tmp->right)
next.push(tmp->right);
if(current.empty()){
if (isRe){
reverse(v.begin(), v.end());
}
ret.push_back(v);
swap(current,next);
isRe = !isRe;
v.clear();
}
}
}
};
LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]的更多相关文章
- 103. Binary Tree Zigzag Level Order Traversal (Tree, Queue; BFS)
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 ...
- 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 [LeetCode] 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 ...
- 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
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 ...
随机推荐
- baas & API 网关
最近一段时间一直在做API 网关的工作.清晰看到当前云下Baas将会是主要方向,而API网关会是一把利剑. 本人正在规划API网关,有兴趣的可以一起探讨:hotwheels_bo@163.com
- 基于visual Studio2013解决面试题之0701寻找丑数
题目
- 前端javascript框架之BackboneJS学习笔记
<!DOCTYPE html><html><head><meta charset="utf-8"><script src=&q ...
- JDBC操作数据库的学习(2)
在上一篇博客<JDBC操作数据库的学习(1)>中通过对例1,我们已经学习了一个Java应用如何在程序中通过JDBC操作数据库的步骤流程,当然我们也说过这样的例子是无法在实际开发中使用的,本 ...
- span设置固定宽度
<span> 标签是被用来组合文档中的行内元素.相信对一般的网页设计师来讲是非常熟悉的朋友了,使用相当频繁,但我们往往很少对SPAN设定样式,一般也没什么必要,大多数都留给DIV老朋友了. ...
- [置顶] oracle 数据库表中转换成java代码
--数据库中字段java代码 select col.TABLE_NAME,replace(initcap(col.TABLE_NAME),'_', '') , 'private '||decode ...
- Ubuntu下装QQ2012,让linux小白们不怕脱离windows
嘿嘿,很多人可能跟我一样,QQ上同学群里会通知一些事项,所以我们希望可以在linux下开QQ,但是QQ官网做的QQ For Linux, 实在是烂的不行 那么怎么在linux下装我们平时在window ...
- 查看进程所用的内存(使用GetWindowThreadProcessId取得进程ID,OpenProcess打开进程和GetProcessMemoryInfo取得内存信息)
// function GetProcessMemorySize(_sProcessName: string; var _nMemSize: Cardinal): Boolean; var l_nWn ...
- 得到一个div下 特定ID的所有标签
比如说得到 <div id="showsp"> <div id="a"></div> <div id="a& ...
- c/c++ extern “C”
c/c++ extern “C” 常见的样式 extern “C”{ ... } extern "C" return-type func-name(type , type ){} ...