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]
]
/**
* 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 == NULL){
vector<vector<int> >ans();
return ans;
} queue<pair<TreeNode*,int> >q;
q.push(make_pair(root,)); vector<vector<int> >ans; while(!q.empty()){
TreeNode* t=q.front().first;
int depth=q.front().second; if(ans.size()==depth){
ans.push_back(vector<int>());
} ans[depth].push_back(t->val); if(t->left!=NULL){
q.push(make_pair(t->left,depth+));
}
if(t->right!=NULL){
q.push(make_pair(t->right,depth+));
}
q.pop();
} return ans; }
};

BFS法

/**
* 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> >ans; void dfs(TreeNode* r,int depth){
if(r==NULL) return ; if(ans.size()==depth){
ans.push_back(vector<int>());
}
ans[depth].push_back(r->val); dfs(r->left,depth+);
dfs(r->right,depth+);
} vector<vector<int> > levelOrder(TreeNode* root) {
dfs(root,);
return ans;
} };

DFS法

leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)的更多相关文章

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

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

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

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

  4. leetcode 102 Binary Tree Level Order Traversal ----- java

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

  5. Java [Leetcode 102]Binary Tree Level Order Traversal

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

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

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

  8. Java for 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, ...

  9. 【LeetCode】Binary Tree Level Order Traversal 【BFS】

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

随机推荐

  1. 开发SharePoint 自定义WebService 的小工具

    是一个开源的项目,地址:http://www.codeproject.com/Articles/10728/WSS-Web-Service-DISCO-and-WSDL-Generator-Helpe ...

  2. MySQL -进阶

    一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用 SELECT * FROM(SELE ...

  3. 11-BeautifulSoup库详解

    ---恢复内容开始--- 灵活又方便的网页解析库,处理高效,支持多种解析器. 利用它不用编写正则表达式即可方便地实现网页信息的提取. 这个库有四个主要方法吧,其中xlml是最常用的,他的标签选择器可以 ...

  4. java nio读取和写入文件

    读取 package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...

  5. java 匿名类和匿名方法

    package com.test; interface product{ int getPrice(); } public class News { /** * @param args */ publ ...

  6. Linux内核编译过程分析

    http://pan.baidu.com/s/1mgtACVu 其中是我总结生成的一些文档,以便于理解当我们输入make uImage后,系统是怎么一步一步生成uImage的,我采用的是逆向分析的方法 ...

  7. JavaWeb学习总结第一篇--初识JavaWeb

    JavaWeb学习总结(一)-- 初识JavaWeb 一:Web相关概念 Web程序也就是一般所说的网站,由服务器.客户端浏览器和网络组成.Web程序的好处就是使用简单,不需要安装.学习,有一台电脑. ...

  8. jquery实现重置

    $('#reset').click(function(){ $('#info_frm')[0].reset(); });

  9. Android_YouthArea之ApeendTextView

    这次给我自己的项目打个广告:http://sj.qq.com/myapp/detail.htm?apkName=com.youthcommunity 这款APP 不同于SoHOT是积极的,是年轻人的信 ...

  10. Windows系统下正确安装MongoDB

    1.下载.安装 官网下载: http://www.mongodb.org/downloads 下载好之后,接下来进行安装了: 2.创建数据文件夹 MongoDB将数据文件夹存储在 db 文件夹下. 可 ...