【Leetcode】Binary Tree Level Order Traversal
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 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>> result; if(root == NULL) return result; queue<TreeNode *> lq;
lq.push(root);
int curlc = 1;
int nextlc = 0; while(!lq.empty())
{
vector<int> level;
while(curlc > 0)
{
TreeNode * temp = lq.front();
lq.pop();
curlc--;
level.push_back(temp->val); if(temp->left)
{
lq.push(temp->left);
nextlc++;
}
if(temp->right)
{
lq.push(temp->right);
nextlc++;
}
} curlc = nextlc;
nextlc = 0;
result.push_back(level);
} return result;
}
};
【Leetcode】Binary Tree Level Order Traversal的更多相关文章
- 【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, ...
- 【题解】【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, ...
- 【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 ...
- 【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, ...
- 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)
这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...
- 【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 ...
- 【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, ...
- 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 ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
随机推荐
- 一些Swift编程语言的相关资料
苹果官方Swift文档<The Swift Programming Language> 苹果开发者Swift文档及介绍 中文版Apple官方Swift教程(Github协作翻译中) Git ...
- Java Annotation手册
Java Annotation手册 作者:cleverpig(作者的Blog:http://blog.matrix.org.cn/page/cleverpig) 原文:http://www.matri ...
- FZU 1753
题目的思路还是很简单的,找出这些组合数中最大的公约数: 其中C(n,k)=n ! /k!/(n-k)! 所以枚举每个素因数,用(n!)的减去(k!)和(n-k)!的就行了... 最后取每组的最小值 # ...
- EasyUI 树形菜单tree 定义图标
{ "id":1, "text":"Folder1", "iconCls":"icon-save", ...
- vlc
源码下载地址:http://download.videolan.org/pub/videolan/vlc/ 编译依赖: sudo apt-get install liblua5.2-dev sudo ...
- 2.5.6 使用progressDialog创建进度对话框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- vs2010 dll生成,使用问题[good]
VS2010 动态库开发——第一章 演练:创建和使用动态链接库 (C++) 转载自[http://www.cnblogs.com/sdlypyzq/archive/2012/01/17/2324215 ...
- index 辨别字符在字符串中的位置
namespace index{ class Program { static void Main(string[] args) { wh ...
- WordPress Sharebar ‘page’参数跨站脚本漏洞
漏洞名称: WordPress Sharebar ‘page’参数跨站脚本漏洞 CNNVD编号: CNNVD-201309-468 发布时间: 2013-09-26 更新时间: 2013-09-26 ...
- [C# 网络编程系列]专题七:UDP编程补充——UDP广播程序的实现
转自:http://www.cnblogs.com/zhili/archive/2012/09/03/2666974.html 上次因为时间的关系,所以把上一个专题遗留下的一个问题在本专题中和大家分享 ...