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即可

class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> Q;
vector<vector<int>> ans;
Q.push(root);
while(!Q.empty())
{
int e = Q.size(); //当前层的结束位置
vector<int> partans;
for(int i = ; i < e; i++)
{
TreeNode * p = Q.front();
Q.pop();
if(NULL != p)
{
partans.push_back(p->val);
Q.push(p->left);
Q.push(p->right);
}
}
if(!partans.empty())
ans.push_back(partans);
}
return ans;
}
};

网上DFS的代码:

class Solution {
protected:
vector<vector<int>> ans;
void dfs(TreeNode *root, int height){
if (root == NULL)
return;
while (ans.size() <= height)
ans.push_back(vector<int>());
ans[height].push_back(root->val);
dfs(root->left, height + );
dfs(root->right, height + );
} public:
vector<vector<int>> levelOrder(TreeNode* root) {
dfs(root, );
return ans;
}
};

Binary Tree Level Order Traversal II

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

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

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

跟上面只是顺序变了,加个reverse就行了。

【leetcode】Binary Tree Level Order Traversal I & II的更多相关文章

  1. 【题解】【BT】【Leetcode】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 II

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

  3. 【Leetcode】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】Binary Tree Level Order Traversal 【BFS】

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

  5. 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)

    这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...

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

  7. 【Leetcode】【Easy】Binary Tree Level Order Traversal

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

  8. Java for LeetCode 107 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 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

随机推荐

  1. JQuery中的html(),text(),val()区别

    jQuery中.html()用为读取和修改元素的HTML标签,.text()用来读取或修改元素的纯文本内容,.val()用来读取或修改表单元素的value值. 1.HTML html():取得第一个匹 ...

  2. informatica中元数据管理

    摘自: http://blog.itpub.net/28690368/viewspace-766528/ informaica是一个很强大的ETL工具,WORKFLOW MANAGER负责对ETL调度 ...

  3. Ubuntu 12 升级 SVN 1.6 到 1.8 版本

    在 Ubuntu 12 中使用 PhpStorm 10.x,CheckOut项目后,Event Log 提示: Subversion command line client version is to ...

  4. C#之XMAL与WPF

    XAML的简单说明 XAML是用于实例化.NET对象的标记语言,主要用于构建WPF的用户界面 XAML中的每一个元素都映射为.NET类的一个实例,例如<Button>映射为WPF的Butt ...

  5. 错误:The Controls collection cannot be modified because the control contains code blocks (i.e. ). .

    用 <%# %>这种写法是写在数据绑定控件中的,之所以用 <%= %>会出现The Controls collection cannot be modified because ...

  6. jquery mobile cannot be created in a document with origin 'null' and URL

    jquery mobile cannot be created in a document with origin 'null' and URL http://zhidao.baidu.com/lin ...

  7. jQuery中添加自定义或函数方法

    <script type="text/javascript"> (function () { $.fn.parHy = function (val) { alert(v ...

  8. .Net的要知道的一些事

    1.什么是.NET?什么是CLI?什么是CLR?IL是什么?JIT是什么,它是如何工作的?GC是什么,简述一下GC的工作方式? .Net是微软推出的框架 CLI是公共语言接口(规范) CLR是公共语言 ...

  9. leetcode 236. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  10. JavaScript break跳出多重循环

    多重循环在编程中会经常遇到,那么在JavaScript中如何指定跳出那层的循环呢.其实这也是break的一个用法,下面是一个不错的例子,来自<JavaScript权威指南>,可以参考下: ...