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解题思路:

使用二叉树按层遍历的方法,很容易解决。

 /**
* Definition for binary tree
* 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) {
vector<vector<int> > levelOrderLst;
vector<int> valLst;
queue<TreeNode *> curLevelNodes; if (!root)
return levelOrderLst; curLevelNodes.push(root); while (!curLevelNodes.empty()) {
int len = curLevelNodes.size();
while (len--) {
valLst.push_back(curLevelNodes.front()->val); if (curLevelNodes.front()->left) {
curLevelNodes.push(curLevelNodes.front()->left);
} if (curLevelNodes.front()->right) {
curLevelNodes.push(curLevelNodes.front()->right);
} curLevelNodes.pop();
} levelOrderLst.push_back(valLst);
valLst.clear();
} return levelOrderLst; } };

DFS解题思路:

[                 [
[3], [3],
[9], ==> [9,20],
[15] [15,7]
] ]

树中,第一层val值保存在vec[0],第N层val值保存在vec[n-1]。因此按DFS原则,可以统一使用“先左后右”的规律,先遍历左子树,再遍历右子树。遍历到的val值插入对应dep的位置中。

注意:将val插入对应dep位置前,此位置上需要存在子列表,才能执行插入命令(代码中10-11行)。

 class Solution {
private:
vector<vector<int> > levelOrderLst;
public:
void buildVector(TreeNode *root, int depth)
{
if (root == NULL)
return; if (levelOrderLst.size() == depth)
levelOrderLst.push_back(vector<int>()); levelOrderLst[depth].push_back(root->val); buildVector(root->left, depth + );
buildVector(root->right, depth + );
} vector<vector<int> > levelOrder(TreeNode *root) {
buildVector(root, );
return levelOrderLst;
}
};

附录:

DFS/BFS

queue\vector

【Leetcode】【Easy】Binary Tree Level Order Traversal的更多相关文章

  1. Leetcode PHP题解--D125 107. Binary Tree Level Order Traversal II

    val = $value; } * } */ class Solution { private $vals = []; /** * @param TreeNode $root * @return In ...

  2. 【Leetcode】【Easy】Binary Tree Level Order Traversal II

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

  3. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  4. 【leetcode】Binary Tree Level Order Traversal I & II

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

  5. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  6. 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)

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

  7. 【Binary Tree Level Order Traversal II 】cpp

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

  8. 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】

    Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...

  9. LeetCode:Binary Tree Level Order Traversal I II

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

  10. LeetCode(32)-Binary Tree Level Order Traversal

    题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...

随机推荐

  1. 欧拉图 欧拉回路 欧拉通路 Euler的认识 (转)

    转:https://www.cnblogs.com/Ash-ly/p/5397702.html 定义: 欧拉回路:图G的一个回路,如果恰通过图G的每一条边,则该回路称为欧拉回路,具有欧拉回路的图称为欧 ...

  2. [转] Spring Boot 揭秘与实战(二) 数据存储篇 - ElasticSearch

    [From] http://www.tuicool.com/articles/JBvQrmj 本文讲解Spring Boot基础下,如何使用 ElasticSearch,实现全文搜索. 版本须知 sp ...

  3. async中series的实现 javascript构件

    //同步流程 var series=function(arr){ function async(i){ arr[i](function(){ if(1+i<arr.length){ async( ...

  4. 集成 Jenkins 和 TestNG 实现自助式自动化测试平台

    背景介绍 在软件业十分成熟的今天,敏捷(Agile)开发在业界日益流行,而面临的挑战也日益增多,不断变化的用户需求.缩短的开发周期.频繁的部署上线.复杂的产品架构和团队组织,如何继续保证软件的质量是一 ...

  5. python练习六十九:urllib爬取练习

    爬取图片,将链接中的图片取出来,并统计一共下载了多少图片 代码: def fetch_pictures(url): headers = {'User-Agent':'Mozilla/5.0 (Wind ...

  6. nfs 问题总结

    1. [root@backup read]# touch r01.txt   touch: cannot touch `r01.txt': Stale file handle   使用共享目录创建文件 ...

  7. MVC参数自动装配

    在拿到一个类型的所有属性以及字段的描述信息后,就可以通过循环的方式,根据这些数据成员的名字去QueryString,Form,Session,Cookie读取所需的数据了. 就是遍历参数,然后用反射遍 ...

  8. Ant利用第三方的task

    转自 http://blog.sina.com.cn/s/blog_3d21e545010006s9.html 一.如何使用第三方任务   Ant可以使用第三方任务,在使用第三方任务之前,需告知Ant ...

  9. vim源码编译启用python

    坑:只指定with-python-config-dir没有指定enable-pythoninterp是没有用的 ./configure --enable-pythoninterp --with-pyt ...

  10. Linux查找命令与find命令详解

    一.文件查找之locate命令 locate :非实时的,查找时根据全系统文件数据库进行的,模糊查找,update 手动生成文件数据库速度快 依赖于updatedb数据库 1 2 3 4 5 6 7 ...