【LeetCode】102 - Binary Tree Level Order Traversal
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的更多相关文章
- 【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 ...
- 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...
- 【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 ...
- 【一天一道LeetCode】#102. Binary Tree Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【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 ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【leetcode】429. N-ary Tree Level Order Traversal
problem 429. N-ary Tree Level Order Traversal solution1:Iteration /* // Definition for a Node. class ...
随机推荐
- python流程控制语句 ifelse - 3
#! /usr/bin/python x = input ('please inut a integer:') x = int(x) : print ('你输入了一个负数') elif x == : ...
- Android Calendar获取年月日时分秒毫秒
开始使用new Date()测试,并用通过date.getMonth(),和date.getDay()获取,不过后来发现这两个访求是jdk1.1版本的,现在已经不用了,而且结果也不正确. ; int ...
- SSIS ->> Logging
SSIS提供了Event Handler之外的另一种方法捕捉Event和获取需要的信息,这种方法是Logging.SSIS的Logging针对不同的组件可以提供比Event Handler更多的Eve ...
- code manager tools svn服务安装配置
svn server 安装配置: 下载地址:http://www.visualsvn.com/server/download/ 然后安装图一步一步前进: 1.点击download now: 2.点击N ...
- 【USACO】
Among the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to plea ...
- GraphicsMagick / ImageMagick缺少lib报错no decode delegate for this image format
下载相应的lib,编译安装就行了 cd ~ #下载包 wget http://www.imagemagick.org/download/delegates/zlib-1.2.7.tar.gz wget ...
- PowerDesigner15使用时的十五个问题
15个问题列表: No.1:是不是一定要从CDM开始设计,然后在进行PDM的设计? NO.2:工具栏palette不见了,如何把它找出来? NO.3: 如何建立与数据库的关联? NO.4: 域和数据项 ...
- Oracle中用一个表的数据更新另一个表的数据
update tbl1 a set (a.col1, a.col2) = (select b.col1, b.col2 from tbl2 ...
- 【Todo】InnoDB、MyISAM、数据库引擎
关于InnoDB和MyISAM引擎的对比,下面这篇讲的挺好 http://www.cnblogs.com/vicenteforever/articles/1613119.html 这一篇关于InnoD ...
- JS function的定义方法,及function对象的理解。
废话篇: 今天看到了Function的内容,各种晕,各种混淆有木有.简直是挑战个人脑经急转弯的极限啊.不过,最终这一难题还是被我攻克了,哇咔咔.现在就把这东西记下来,免得到时候又忘了就悲催了.... ...