【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 返回它从下 ...
随机推荐
- jersey REST的接口简述
GET方法:只读,是幂等和安全的.在接口中抽象地定义了资源的请求方法类型后,其全部实现类都无须再定义. HEAD方法:与GET方法相似,只是服务器端的返回值不包括HTTP实体. OPTIONS方法:与 ...
- ubuntu下eclipse突然崩溃,解决办法
rm YOUR_WORKSPACE/.metadata/.plugins/org.eclipse.core.resources/.snap 抛出的一场如下: !SESSION 2013-09-30 1 ...
- 17.1 Replication Configuration 复制配置
17.1 Replication Configuration 复制配置 17.1.1 How to Set Up Replication 17.1.2 Replication Formats 17.1 ...
- JavaScript String支持的辅助format函数+【分页1】
/** ) { && ; i < arguments.length; i++) { : int.Parse(Request.Par ...
- [LeetCode#161] One Edit Distance
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Anal ...
- asp.net Context.User.Identity.Name说明
今天做了一个身份验证页面,基本实现功能,却不能显示当前用户姓名,自己MSDN半天一无所获,问题就在Context.User.Identity.Name:Context是HttpContext类,Use ...
- BZOJ3323: [Scoi2013]多项式的运算
3323: [Scoi2013]多项式的运算 Time Limit: 12 Sec Memory Limit: 64 MBSubmit: 128 Solved: 33[Submit][Status ...
- ADO.NET 增删查改小总结
转自:http://www.cnblogs.com/ashu123/archive/2010/10/10/ado_1.html 三套路-----增删改 1 using System.Data.SqlC ...
- [转]NHibernate之旅(3):探索查询之NHibernate查询语言(HQL)
本节内容 NHibernate中的查询方法 NHibernate查询语言(HQL) 1.from子句 2.select子句 3.where子句 4.order by子句 5.group by子句 实例 ...
- linux下jdk的卸载与安装
JDK的卸载 1.检查jdk的是否安装,显示如下表示安装: [root@localhost ~]# rpm -aq|grep java tzdata-java-2010l-1.el6.noarch j ...