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]
]

Solution:

BFS, 树的层序遍历就是用的queue,因为BFS有一个显著的概念就是分层,必须遍历完本层之后,再去遍历下一层。如果不要求区分每一层的话,那么实际上一个队列就能完成。就像经典的树的层序遍历一样,上层的元素肯定首先放入到队列中,下层的元素一定都在上一层元素遍历完之后,才会被遍历。
当要考虑层数是需要用到两个队列。首先将元素放入q1中,然后对q1中的元素出队列,将其左右子树(如果有的话)放入q2中。当q1遍历完毕之后,说明本层已经遍历完毕了,那么就交换q1和q2,继续遍历新的q1(也就是原来的q2),最终,当两个队列都为空(实际上也就是最后交换完毕之后的q1为空)的时候,整个树就都遍历完毕了

/**
* 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> vec;
queue<TreeNode*> q1,q2;
q1.push(root); while(!q1.empty()){
while(!q1.empty()){
TreeNode *temp=q1.front();
vec.push_back(temp->val);
q1.pop();
if(temp->left)q2.push(temp->left);
if(temp->right)q2.push(temp->right);
}
ret.push_back(vec);
vec.clear();
swap(q1,q2); //swap不需要头文件
}
return ret;
}
};

【LeetCode】102 - Binary Tree Level Order Traversal的更多相关文章

  1. 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  2. 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...

  3. 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...

  4. 【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 ...

  5. 【一天一道LeetCode】#102. Binary Tree Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  6. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  7. 【LeetCode】107 - Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  8. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  9. 【leetcode】429. N-ary Tree Level Order Traversal

    problem 429. N-ary Tree Level Order Traversal solution1:Iteration /* // Definition for a Node. class ...

随机推荐

  1. Java中使用验证码和二维码

    资源 需要:   jelly-core-1.7.0.GA.jar 网站:   http://lychie.github.io/products.html 将下载下来的 jelly-core-1.7.0 ...

  2. ehcache 缓存技术

    一 ehcache API: 1: Using the CacheManager 1.1所有ehcache的使用, 都是从 CacheManager. 开始的.有多种方法创建CacheManager实 ...

  3. SPOJ 78 Marbles 组合数学

    相当于从n-1个位置里面找k-1个位置放隔板 #include <cstdio> #include <cstring> #include <cstdlib> #in ...

  4. BZOJ 1507 Editor(块状链表)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...

  5. BZOJ 2751 容易题

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2751 题意:有一个数列A已知对于所有的A[i]都是1到n的自然数,并且知道对于一些A[i ...

  6. java中String类学习

    java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...

  7. hiho #1332 : 简单计算器 栈+递归

    #1332 : 简单计算器 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 编写一个程序可以完成基本的带括号的四则运算.其中除法(/)是整除,并且在负数除法时向0取整.( ...

  8. Linux同步机制 - 基本概念(死锁,活锁,饿死,优先级反转,护航现象)

    死锁(deadlock) 是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进 ...

  9. HeadFirst Jsp 04 (请求和响应作为servlet)

    servlet 的存在就是为了客服服务, servlet的任务是得到一个客户的请求, 再发回一个响应. 由上图可知, web 容器会在启动后就加载所有的servlet类, 并为之创建实例和初始化 注意 ...

  10. vmware装redhat该光盘无法被挂载

    为了考网工,没办法只能学学linux了,前天在vmware7装redhat 提示该光盘无法被挂载,还以为是光盘错误,换了N个盘,又装了很多次,最后观察到,换了盘之后点确定,里面就提示该光盘无法被挂载, ...