(二叉树 BFS) leetcode102. 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,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
] -----------------------------------------------------------------------------
就是遍历二叉树的的每一层,并且把每一层的数加入到一个数组,返回这个二维数组。 C++代码:
/**
* 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) {
queue<TreeNode*> q;
vector<int> vec;
vector<vector<int> > vecs;
if(!root){
return vecs; //这个vecs指的是空数组。
}
q.push(root);
while(!q.empty()){
vec.clear();
for(int i = q.size();i>;i--){
auto t = q.front();
q.pop();
vec.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
vecs.push_back(vec);
}
return vecs;
}
};
(二叉树 BFS) leetcode102. Binary Tree Level Order Traversal的更多相关文章
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- LeetCode102 Binary Tree Level Order Traversal Java
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- leetcode102 Binary Tree Level Order Traversal
""" Given a binary tree, return the level order traversal of its nodes' values. (ie, ...
- 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...
- (二叉树 BFS) 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] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...
- [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, ...
随机推荐
- ORACLE中关于表的一些特殊查询语句
1: 如何判断字段的值里面:那些数据包含小写字母或大小字母 判断字段NAME的值里面有小写字母的记录 方式1: SELECT NAME FROM TEST WHERE regexp_like(NAME ...
- AngularJS学习之旅—AngularJS Scope作用域(五)
1.AngularJS Scope(作用域) Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Sco ...
- c/c++ 继承与多态 引用有的时候并不能达到多态的效果
继承与多态 引用有的时候并不能达到多态的效果 问题:c++ primer 第五版说,只有指针和引用调用虚函数时才会发生动态绑定(多态).实践一下,发现引用有的时候不能发生多态绑定(多态). 下面的例子 ...
- 面相服务的架构SOA
SOA体系结构是基于服务组件模型,将应用程序的不同功能单元通过定义良好的接口契约联系起来,接口是采用中立方式进行定义的,独立于实现服务的硬件平台,操作系统和编程语言.使得构建在这样的系统中的服务可以以 ...
- 统计 flv视频总时长
在学习孟媛的视频课程.网上能下载的是flv格式.那我在学习之前,我要统计一下这个课程的数量,他会用多长时间,这样方便我在学习过程中不断的回顾,进行时间管理.我大概就可以统计出来这个视频多长时间可以学完 ...
- SQLServer之修改FOREIGN KEY约束
使用SSMS数据库管理工具修改FOREIGN KEY约束 1.连接数据库,选择数据表->右键点击->选择设计(或者展开键,选择要修改的外键,右键点击,选择修改,后面修改步骤相同). 2.在 ...
- Python爬虫 selenium
库的安装 pip3 install selenium 声明浏览器对象 from selenium import webdriver browser = webdriver.Chrome() brows ...
- org.springframework.web.context.support.XmlWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreatio
错误异常: 11-Apr-2019 18:07:14.006 警告 [RMI TCP Connection(5)-127.0.0.1] org.springframework.web.context. ...
- 1.5 下载和安装VMWare
搭建虚拟环境一般都有两种方法,一种是系统自带的虚拟机,还有一种是下载VMware,Win8和Win10都自带有虚拟机,但是都不是自动开启的,所以我们必须手动开启. 一.Win10开启虚拟机 在命令行输 ...
- 在app中从下向上滑动,以找到不在默认第一页的元素
1.在app中,如果第一面没有出现你要的元素,需要下滑拉动到你需要的元素的位置