[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]
]
confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
/**
* 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>> res;
vector<int> levelNode;
queue<TreeNode *> Q;
if(root) Q.push(root);
int count=; //下一层元素的个数
int levCount=; //当前层元素个数,初始为第一层
while( !Q.empty())
{
TreeNode *cur=Q.front();
levelNode.push_back(cur->val);
Q.pop();
levCount--;
if(cur->left)
{
Q.push(cur->left);
count++;
}
if(cur->right)
{
Q.push(cur->right);
count++;
}
if(levCount==)
{
res.push_back(levelNode);
levCount=count;
count=;
levelNode.clear(); //清空levelNode,为下层
}
}
return res;
}
};
方法二:
思路:遍历完一层以后,队列中节点的个数就是二叉树下一层的节点数。实时更新队列中节点的个数,每层的遍历。
/**
* 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>> res;
queue<TreeNode *> Q;
if(root) Q.push(root); while( !Q.empty())
{
int count=;
int levCount=Q.size();
vector<int> levNode; //遍历当前层
while(count<levCount)
{
TreeNode *curNode=Q.front();
Q.pop();
levNode.push_back(curNode->val);
if(curNode->left)
Q.push(curNode->left);
if(curNode->right)
Q.push(curNode->right);
count++;
}
res.push_back(levNode);
}
return res;
}
};
/**
* 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>> res;
queue<TreeNode *> Q;
if(!root) return res;
Q.push(root);
Q.push(NULL);
vector<int> levNode; //存放每层的结点的值 while( !Q.empty())
{
TreeNode *cur=Q.front();
Q.pop();
if(cur)
{
levNode.push_back(cur->val);
if(cur->left)
Q.push(cur->left);
if(cur->right)
Q.push(cur->right);
}
else
{
res.push_back(levNode);
levNode.clear();
if( !Q.empty())
Q.push(NULL);
}
}
return res;
}
};
[Leetcode] Binary tree level order traversal二叉树层次遍历的更多相关文章
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- [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, ...
- 102. Binary Tree Level Order Traversal二叉树层序遍历
网址:https://leetcode.com/problems/binary-tree-level-order-traversal/ 参考:https://www.cnblogs.com/grand ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- [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 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 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
随机推荐
- Xamarin.Forms+Prism(3)—— 简单提示UI的使用
这次给大家介绍两个比较好用的提示插件,如成功.等待.错误提示. 准备: 1.新建一个Prism Xamarin.Forms项目: 2.右击解决方案,添加NuGet包: 1)Acr.UserDialog ...
- 大话PROFINET
1.PROFINET是什么? PROFINET的全称是Process Field Net,是由PROFIBUS国际组织PI(PROFIBUS International)推的出,在IEC61158标准 ...
- MySQL注入与防御(排版清晰内容有条理)
为何我要在题目中明确排版清晰以及内容有条理呢? 因为我在搜相关SQL注入的随笔博客的时候,看到好多好多都是页面超级混乱的.亲爱的园友们,日后不管写博客文章还是平时写的各类文章也要多个心眼,好好注意一下 ...
- phpcms笔记
一.建立虚拟站点 1.先更改www目录下的站点名称,再找到apache, 打开"Apache2\conf\extra"下的"httpd-vhosts.conf" ...
- js闭包绑定元素
闭包,官方对闭包的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.闭包的特点: 1. 作为一个函数变量的一个引用,当函数返回时,其处于激活状 ...
- 工具类总结---(四)---Sharedpreferences保存
用于保存具有对应关系的键值对 import android.content.Context; import android.content.SharedPreferences; import java ...
- VR全景加盟、720全景、VR全景技术平台-全国招商模式疯狂开始
VR全景:互联网与实体店的完美结合 VR元年已过,VR项目.VR创业潮转为理性,VR行业分为两个方向:硬件和内容.硬件又分为VR头显和辅助设备,内容又分为VR全景和VR虚拟内容,如游戏.娱乐.根据行 ...
- vue视频学习笔记01
video 1 vue:读音: v-u-eview vue到底是什么?一个mvvm框架(库).和angular类似比较容易上手.小巧mvc:mvpmvvmmv*mvx官网:http://cn.vuej ...
- cookie值的设置,获取及删除
<script> function setCookie( key, val, expire){ var dateTime = new Date(); dateTime.setTime( d ...
- 【JAVAWEB学习笔记】05_jQuery基础
晨读单词: toggle:切换 each:每个(遍历) append:追加(内部追加,将B追加到A的内部结尾处) appendTo:追加(内部追加,将A追加到B的内部结尾处) prepend:追加(内 ...