网址:https://leetcode.com/problems/binary-tree-level-order-traversal/

参考:https://www.cnblogs.com/grandyang/p/4051321.html

二叉树的层次遍历的做法是维护一个队列,不断使节点入队,使用了val后即出队。结束标志是队列是否为空

主要难度在于要分别提取出每层的val

我们可把在树的一层节点全部入列后队列的长度作为for循环的判断依据

/**
* 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)
return {};
vector<vector<int>> res;
queue<TreeNode*> q;
q.push(root);
vector<int> oneLevel;
while (!q.empty()) {
oneLevel.clear();
// 一轮for循环即是对一层节点的处理
for (int i = q.size(); i > ; --i) {
TreeNode *t = q.front();
q.pop();
oneLevel.push_back(t->val);
if (t->left)
q.push(t->left);
if (t->right)
q.push(t->right);
}
res.push_back(oneLevel);
}
return res;
}
};

102. Binary Tree Level Order Traversal二叉树层序遍历的更多相关文章

  1. [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, ...

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

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

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

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

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

  5. 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 ...

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

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

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

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

  8. Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

    二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

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

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

随机推荐

  1. redis 有序集合(zset)函数

    redis 有序集合(zset)函数 zAdd 命令/方法/函数 Adds the specified member with a given score to the sorted set stor ...

  2. centos7安装git

    1.安装git依赖包 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUti ...

  3. 【转】Anaconda下安装pyecharts步骤及常见错误

    本文转载自:https://blog.csdn.net/skj1995/article/details/81187954 (1)之前看了几篇博客,有人说用cmd命令在目录C:\Users\Admini ...

  4. 服务器告警其一:硬盘raid问题

    问题描述 服务器一直间断发出告警音,但是根据raid类型的不同有一定可能进入系统. 问题详情 在LSI Mega Webbios自检之后系统开始出现告警音. 在Lsi Mega Webbios的ini ...

  5. AtomicStampedReference源码分析

    public class Snippet { //修改的是AtomicStampedReference对象里面的值了. public static void main(String[] args) { ...

  6. 当namenode启动不了时

    重新格式即可,注意格式化前要将临时目录下的文件全部删除

  7. postman抓包工具与kap项目部署(新猿旺学习总结)

    postman抓包工具 1.post请求在哪里输入数据:选择请求方法-post--->在body里面如图位置输入参数和值,如果是json格式在raw出填写 get请求在哪里输入数据:选择请求方法 ...

  8. 我的web前端整理和学习

    知识点收藏:(边看.边记录.边写) 开直播学习:虎牙 待办事理>> 练习自我表达(把文章做成视频).技术学习总结(博客与公众号).跳出舒适圈. 前端知识体系:https://www.cnb ...

  9. 高级shell 脚本

    1.函数 函数是一个脚本代码块,你可以为其命名并在代码中任何位置重用.要在脚本中使用该代码块时,只要使用所起的函数名就行了(这个过程称为调用函数).本节将会介绍如何在shell脚本中创建和使用函数 创 ...

  10. Django多表查询

    一.前言 1.什么是ORM? ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在具体的操作业务对象的时候, ...