题面

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

层序遍历二叉树,要求从上到下,从左到右,输出结果为二维vector。

样例

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

算法

想法:还记得怎么进行层序遍历吗?结果是一个一维数组。现在的情景是要把每一层用一个单独的vector存起来,仅此而已。那么我们的首要问题就是,怎么解决“一层”的问题。

其实,我们在处理完上一层时,queue中就会只包含有本层的节点指针,那么我们计算queue.size(),然后只处理queue的前queue.size()个元素不就可以了吗?在处理其中每一个元素的时候,判空。非空的话,值压入vector,然后把左孩子和有孩子都再次压入queue,最后queue.pop()。处理完这一层,就把当前vector整个压入结果vector<vector<int>> res 中。

1. 根节点判空,若空,返回空vector<vector<int>> (); 不空,继续

2. 新建queue<TreeNode*> q, 和结果vector<vector<int>> res,根节点压入q中

3. 若q非空,while循环判断

新建vector。计算q元素个数,即为一层节点,逐个判空,若不空,值压入vector。然后把子节点压入q中,然后把它弹出queue。

处理完这层,若vector不空,整个压入res中。

4. 返回 res 结束。

源码

 /**
* 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) {
//层序遍历
if(root == nullptr)
return vector<vector<int>> (); vector<vector<int>> res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int size = q.size();
vector<int> tmp;
while(size--)
{
TreeNode* p = q.front();
if(p != nullptr)
{
tmp.push_back(p->val);
q.push(p->left);
q.push(p->right);
}
q.pop();
}
if(tmp.size() != )
res.push_back(tmp);
}
return res;
}
};

leetcode-102.层序遍历二叉树(正序)· BTree的更多相关文章

  1. 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出

    用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...

  2. Python3 解析XML 层序遍历二叉树

    Python3 解析XML 层序遍历二叉树 keyword : python3, xml, xml.dom.minidom, 层序遍历, 层次遍历, 二叉树 part1 问题描述 面对如下 XML 文 ...

  3. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  4. 层序遍历二叉树 完整层序重建二叉树 python

    给定一个二叉树的完整的层次遍历序列(包含所有节点,包括空节点),利用这个序列生成一颗二叉树. 我们首先来看怎样对一颗二叉树进行层序遍历,下图所示的二叉树层次遍历的结果为[a,b,c,d,e],在这个过 ...

  5. UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)

    题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...

  6. Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>

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

  7. Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树

    题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...

  8. PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  9. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

随机推荐

  1. [MongoDB教程] 2.MongoDB的安装与使用

    下载mongodb的版本,两点注意 根据业界规则,偶数为稳定版,如3.2.X:奇数为开发版,如3.3.X 32bit的mongodb最大只能存放2G的数据,64bit就没有限制 MongoDB官网安装 ...

  2. linux vim基本操作

    vim 是一款功能强大的文本编辑器, 默认有三种模式: 命令模式, 插入模式, 编辑模式, 使用# vim file 打开一个文件时,默认进入命令模式, 不同模式直接的切换如下    a. 命令模式切 ...

  3. eclipse spring3.X redis 整合-配置

    花了一天时间折腾redis的配置 用到的jar spring 3.1.1 aopalliance-1.0.jar commons-pool2-2.3.jar jedis-2.7.2.jar sprin ...

  4. 重新学习微信小程序

    基础学习: 传送门:http://www.jianshu.com/p/1cec15a81722 这个简书博客介绍的很详细,今天思思重新学习了一下. 一路到最后只遇到一个坑,还是自己不仔细.这里记录下: ...

  5. Spring Boot学习笔记——Spring Boot与ActiveMQ的集成

    Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.这里以ActiveMQ为 ...

  6. json转字符串 —— jsonObj.toJSONString()与JSON.stringify(jsonObj)json to string

    json 转变成 string方法介绍: var people = { "programmers": [{ "firstName": "Brett&q ...

  7. Tensorflow 2.0 datasets数据加载

    导入包 import tensorflow as tf from tensorflow import keras 加载数据 tensorflow可以调用keras自带的datasets,很方便,就是有 ...

  8. TiKV事务实现浅析

    TiKV事务实现浅析 Percolator事务的理论基础 Percolator的来源 Percolator事务来源于Google在设计更新网页索引的系统时提出的论文Large-scale Increm ...

  9. Asp.Net文件的上传和下载

    aspx页面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="上传和下载文件. ...

  10. 日常工作问题解决:配置NTP服务器以及一些常见错误解决

    1.配置NTP服务端 环境:redhat 6.5 服务器主机名 ip地址 说明 server 192.168.57.20 NTP服务端 client 192.168.57.21 NTP客户端 搭建说明 ...