【Binary Tree Level Order Traversal】cpp
题目:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int> > ret;
if (!root) return ret;
vector<int> tmp_ret;
deque<TreeNode *> currLevel, nextLevel;
currLevel.push_back(root);
while ( !currLevel.empty() )
{
while ( !currLevel.empty() )
{
TreeNode * tmp = currLevel.front();
currLevel.pop_front();
tmp_ret.push_back(tmp->val);
if ( tmp->left ) nextLevel.push_back(tmp->left);
if ( tmp->right ) nextLevel.push_back(tmp->right);
}
ret.push_back(tmp_ret);
tmp_ret.clear();
std::swap(currLevel, nextLevel);
}
return ret;
}
};
tips:
核心:两个队列技巧
1. 采用两个队列,一个队列存放本层TreeNode,另一个队列存放下一层的TreeNode
2. 本层Node逐个出队的同时加入tmp_ret的vector,下一层的Node逐个入队
3. 本层Node全部出队之后,tmp_ret推入ret(并注意清空tmp_ret,第一次没有清空tmp_ret没有AC)
4. 逐个时候currLevel队列已经空了,nextLevel队列存放的都是下一层的节点,利用swap操作交换二者。(这个交换是O(1)的指针交换)
完毕。
======================================
一些关于二叉树 deque queue的参考资料:
http://www.cnblogs.com/way_testlife/archive/2010/10/07/1845264.html
http://stackoverflow.com/questions/2247982/deque-vs-queue-in-c
=======================================
第二次过这道题,用双队列的思路实现的,代码一次AC。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int> > ret;
queue<TreeNode*> curr;
queue<TreeNode*> next;
if ( root ) curr.push(root);
while ( !curr.empty() )
{
vector<int> tmp;
while ( !curr.empty() )
{
tmp.push_back(curr.front()->val);
if ( curr.front()->left ) next.push(curr.front()->left);
if ( curr.front()->right ) next.push(curr.front()->right);
curr.pop();
}
ret.push_back(tmp);
std::swap(next, curr);
}
return ret;
}
};
【Binary Tree Level Order Traversal】cpp的更多相关文章
- 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】
Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...
- 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...
- 【Binary Tree Post order Traversal】cpp
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 【遍历二叉树】05二叉树的层次遍历II【Binary Tree Level Order Traversal II】
就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【Leetcode】【Easy】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...
随机推荐
- Android开发教程大全介绍
Android是由谷歌在2007年推出的一个开放系统平台,主要针对移动设备市场,目前版本为Android 4.0.Android基于Linux,开发者可以使用Java或C/C++开发Android应用 ...
- 最简单删除SQL Server中所有数据的方法
最简单删除SQL Server中所有数据的方法 编写人:CC阿爸 2014-3-14 其实删除数据库中数据的方法并不复杂,为什么我还要多此一举呢,一是我这里介绍的是删除数据库的所有数据,因为数据之间 ...
- LevelDB:一个快速轻量级的key-value存储库(译)
作者:Jeff Dean, Sanjay Ghemawat 原文:http://leveldb.googlecode.com/svn/trunk/doc/index.html 译者:phylips@b ...
- php生成随机字符串和验证码的类
网上有很多的php随机数与验证码的代码与文章,真正适用的没有几个. 索性自己搞一个吧. 开始本节的php教程 吧,以下代码的实现,主要做到可以很好区分一个get_code(),另一个create_ch ...
- Adapter的getView方法详解
来自:http://blog.csdn.net/yelbosh/article/details/7831812 BaseAdapter就Android应用程序中经常用到的基础数据适配器,它的主要用途是 ...
- Java命名:
如果没有public类,就和遵循文件名命名规则: 1.一个.java文件只能有1个public class(暗示可以没有) 2.如果有public class,那么文件名必须与修饰符为public的类 ...
- [FAQ]String(字串相連)與StringBuilder的差別、原理與優缺點?
原文位於 http://www.dotblogs.com.tw/mis2000lab/archive/2013/09/09/msdn_string_stringbuilder.aspx [FAQ]St ...
- SQLServer2005中查询语句的执行顺序
SQLServer2005中查询语句的执行顺序 --1.from--2.on--3.outer(join)--4.where--5.group by--6.cube|rollup--7.havin ...
- (转)Linux下用mkisofs制作光盘镜像ISO文件
我们都知道在windows下有winiso可以将光盘制作成光盘镜像ISO文件,在linux下一个命令就搞定了.那就是mkisofs.先看看mkisofs的help. rory@dev:~$ mkiso ...
- 用Java简单实现C#的参数为Action<T> Function<T,boolean>扩展方法
直接上代码 Blog.Java public class Blog { public Blog(int id,String name) { Id=id; Name=name; } public int ...