这道题是LeetCode里的第102道题。

题目要求:

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:

给定二叉树: [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

返回其层次遍历结果:

[
[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) {
queue<TreeNode*>qt;
TreeNode *pt;
vector<vector<int>>tres;
vector<int>ores;
if(root==NULL)
return tres;
pt=root;
qt.push(pt);
while(qt.size()!=0){
int size=qt.size();
for(int i=0;i<size;i++){
pt=qt.front();
if(pt!=NULL){
if(pt->left!=NULL)qt.push(pt->left);
if(pt->right!=NULL)qt.push(pt->right);
ores.push_back(pt->val);
}
qt.pop();
}
tres.push_back(ores);
ores.clear();
}
return tres;
}
};

提交结果:

个人总结:

层次遍历和前序遍历中序遍历后序遍历不同,它使用的是队列,因为层次遍历的要求是当遍历完上一层后才可以遍历下一层,这符合队列的思想,或者可以称为广度优先搜索。

【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)的更多相关文章

  1. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

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

  2. leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历

    基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...

  3. 102 Binary Tree Level Order Traversal 二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7],    3   / \  9  20    ...

  4. Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...

  5. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

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

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

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

  7. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  8. [Leetcode] Binary tree level order traversal二叉树层次遍历

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

  9. LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

随机推荐

  1. SQL Server 2016,2014 “无法找到数据库引擎启动句柄”

    当我决定安装SharePoint 2016 IT预览版时,我想我应该将它安装在Windows Server 2016技术预览版以及SQL Server 2016社区技术预览版(CTP)上.我敢打赌,你 ...

  2. CAS 配置NLB 负载均衡网络无法连接

    在虚拟机与虚拟机.虚拟机与实机之间利用Windows操作系统自带的网络负载均衡功能如选择单播集群模式,网络就无法通讯,NLB不成功. Scenario #1 在虚拟机与虚拟机之间选择多播模式NLB可正 ...

  3. svn与git区别简介,git分支操作在mac客户端soureTree和使用命令行如何实现

    svn与git区别简介: 性能方面(经过实践的) svn:下载速度慢,因为它其中的源文件太多,并且在show log日志的时候每次都需要去服务器拉取,速度很慢 git:下载速度快,并且git clon ...

  4. 【LeetCode】9 Palindrome Number 回文数判定

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  5. jsp另外五大内置对象之response-设置头信息

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  6. Modelsim与Simulink协同仿真

    当使用硬件描述语言(HDL)完成电路设计时,往往需要编写Testbench对所设计的电路进行仿真验证,测试设计电路的功能是否与预期的目标相符.而编写Testbench难度之大,这时可以借助交互式图形化 ...

  7. 从照片网站pexels批量爬取照片

    调试中,未成功. from bs4 import BeautifulSoup import requests headers={ #'User-Agent':'Nokia6600/1.0 (3.42. ...

  8. 如何使用ABAP Restful API进行代码的全文搜索

    使用这个代码全文搜索的前提条件,是在事务码SFW5里激活业务功能:SRIS_SOURCE_SEARCH 只需要把这个url贴到浏览器里: https://:44355/sap/bc/adt/repos ...

  9. 使用Java connector消费ABAP系统的函数

    Java Connector(JCO)环境的搭建:Step by step to download and configure JCO in your laptop 我的ABAP系统有个函数名叫ZDI ...

  10. UVA 1451 Average平均值 (数形结合,斜率优化)

    摘要:数形结合,斜率优化,单调队列. 题意:求一个长度为n的01串的子串,子串长度至少为L,平均值应该尽量大,多个满足条件取长度最短,还有多个的话,取起点最靠左. 求出前缀和S[i],令点Pi表示(i ...