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

层序遍历二叉树是典型的广度优先搜索 BFS 的应用,但是这里稍微复杂一点的是,要把各个层的数分开,存到一个二维向量里面,大体思路还是基本相同的,建立一个 queue,然后先把根节点放进去,这时候找根节点的左右两个子节点,这时候去掉根节点,此时 queue 里的元素就是下一层的所有节点,用一个 for 循环遍历它们,然后存到一个一维向量里,遍历完之后再把这个一维向量存到二维向量里,以此类推,可以完成层序遍历,参见代码如下:

解法一:

class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if (!root) return {};
vector<vector<int>> res;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
vector<int> oneLevel;
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;
}
};

下面来看递归的写法,核心就在于需要一个二维数组,和一个变量 level,关于 level 的作用可以参见博主的另一篇博客 Binary Tree Level Order Traversal II 中的讲解,参见代码如下:

解法二:

class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
levelorder(root, , res);
return res;
}
void levelorder(TreeNode* node, int level, vector<vector<int>>& res) {
if (!node) return;
if (res.size() == level) res.push_back({});
res[level].push_back(node->val);
if (node->left) levelorder(node->left, level + , res);
if (node->right) levelorder(node->right, level + , res);
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/102

类似题目:

Binary Tree Level Order Traversal II

Binary Tree Zigzag Level Order Traversal

Minimum Depth of Binary Tree

Binary Tree Vertical Order Traversal

Average of Levels in Binary Tree

N-ary Tree Level Order Traversal

参考资料:

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

https://leetcode.com/problems/binary-tree-level-order-traversal/discuss/33445/Java-Solution-using-DFS

https://leetcode.com/problems/binary-tree-level-order-traversal/discuss/33450/Java-solution-with-a-queue-used

https://leetcode.com/problems/binary-tree-level-order-traversal/discuss/114449/A-general-approach-to-level-order-traversal-questions-in-Java

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 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. 102. Binary Tree Level Order Traversal二叉树层序遍历

    网址:https://leetcode.com/problems/binary-tree-level-order-traversal/ 参考:https://www.cnblogs.com/grand ...

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

  5. 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树

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

  6. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

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

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

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

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

  9. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

随机推荐

  1. VS2015企业版,社区版,专业版详细对比

    VS2015 微软出了3个大版本,其实在前天晚上就放出了三个版本的对比说明.,但是昨天挂掉了..今天特意去看了..截取了自己觉得比较重要的分享一下. 首先我们最常用的 诊断调试工具 其次测试工具(区别 ...

  2. CSS制作三角形和按钮

    CSS制作三角形和按钮 用上一篇博文中关于边框样式的知识点,能制作出三角形和按钮. 我先说如何制作三角形吧,相信大家在平时逛网站的时候都会看到一些导航栏中的三角形吧,比如说: 网易首页的头部菜单栏中, ...

  3. 生成随机id对比

    生成随机id 最近公司的项目游戏生成的随机不重复id,重复概率有点大, 代码如下: private static int id = 0; public static int serverID = 0; ...

  4. 一步一步开发Game服务器(五)地图寻路

    目前大多数使用的寻路算法有哪些? 目前市面上大部分游戏的寻路算法是A*,或者B*. A*通常所说的是最优算法也就是寻找最短路径.B*碰撞式算法也就是,也就是不断的去碰撞能走就走,不管是不是绕路.当然以 ...

  5. [原创]django+ldap实现统一认证部分二(python-ldap实践)

    前言 接上篇文章 [原创]django+ldap实现统一认证部分一(django-auth-ldap实践) 继续实现我们的统一认证 python-ldap 我在sso项目的backend/lib/co ...

  6. <精通JavaScript>---阅读笔记01

    下面是阅读精通JavaScript书做的相关笔记. JS中的函数重载 函数重载必须依赖两件事情:判断传入参数数量的能力和判断传入参数类型的能力,在js中每个函数都带有一个仅在这个函数范围内作用的变量, ...

  7. IOS 2D游戏开发框架 SpriteKit-->续(创建用户角色精灵--原创)

    一.主要实现   今天spritekit实现创建玩家角色精灵(SKSpriteNode *), 增加角色精灵的手势操作,这里增加的手势计算方法与objective-c中是不一样的,因为objectiv ...

  8. 新手,Visual Studio 2015 配置Boost库,如何编译和选择,遇到无法打开文件“libboost_thread-vc140-mt-gd-1_63.lib“的解决办法

    1,到官网下载最新的boost,www.boost.org 这里我下载的1-63版本. 2,安装,解压后运行bootstrap.bat文件.稍等一小会就OK. 3,编译boost库.注意一定要使用VS ...

  9. Java--String 和StringBuilder、StringBuffer 的区别?

    1.String是只读字符串,引用的字符串内容是无法改变的. 2.StringBuffer和StringBuilder的字符串对象可以直接进行修改. 3.StringBuilder没有被synchro ...

  10. mysql 插入数据失败防止自增长主键增长的方法

    mysql设置了自增长主键ID,插入失败的那个自增长ID也加一的,比如失败5个,下一个成功的不是在原来最后成功数据加1,而是直接变成加6了,失败次数一次就自动增长1了,能不能让失败的不增长的? 或者说 ...