当前位置:网站首页>力扣102题:二叉树的层序遍历
力扣102题:二叉树的层序遍历
2022-07-06 23:59:00 【瀛台夜雪】
力扣102题:二叉树的层序遍历
题目描述
给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
输入输出样例
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
输入:root = [1]
输出:[[1]]
输入:root = []
输出:[]
解法1,使用队列利用迭代的方式
vector<vector<int>> levelOrder(TreeNode* root)
{
vector<vector<int>>res;
if(!root)
{
return res;
}
//建立队列
queue<TreeNode *>que;
que.push(root);
vector<int>tempList;
while(!que.empty())
{
int length=que.size();
res.push_back({
});
for(int i=0;i<length;i++)
{
TreeNode * temp=que.front();
que.pop();
// cout<<temp->val<<" ";
res.back().push_back(temp->val);
if(temp->left)
{
que.push(temp->left);
}
if(temp->right)
{
que.push(temp->right);
}
}
}
return res;
}
解法二,使用递归的方式进行
vector<vector<int>>nums;
//使用递归的方法进行解决
void dns(TreeNode *root,int lever)
{
if(!root)
{
return;
}
if(nums.size()==lever)
{
nums.push_back({
});
}
nums[lever].push_back(root->val);
dns(root->left,lever+1);
dns(root->right,lever+1);
}
vector<vector<int>> levelOrder2(TreeNode* root)
{
dns(root,0);
return nums;
}
边栏推荐
- Go 语言的 Context 详解
- Taobao commodity details page API interface, Taobao commodity list API interface, Taobao commodity sales API interface, Taobao app details API interface, Taobao details API interface
- Web Authentication API兼容版本信息
- Aidl and service
- 判断文件是否为DICOM文件
- K6el-100 leakage relay
- DOM node object + time node comprehensive case
- How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
- Leakage relay llj-100fs
- 分布式事务解决方案之TCC
猜你喜欢
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
K6el-100 leakage relay
The navigation bar changes colors according to the route
Under the trend of Micah, orebo and apple homekit, how does zhiting stand out?
基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
sql查询:将下一行减去上一行,并做相应的计算
Photo selector collectionview
JVM (19) -- bytecode and class loading (4) -- talk about class loader again
Record a pressure measurement experience summary
Flink SQL 实现读写redis,并动态生成Hset key
随机推荐
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface
Mybaits multi table query (joint query, nested query)
Most commonly used high number formula
Message queue: how to handle repeated messages?
Web architecture design process
Digital innovation driven guide
What are the common message queues?
爬虫练习题(三)
4. Object mapping Mapster
If you want to choose some departments to give priority to OKR, how should you choose pilot departments?
删除文件时提示‘源文件名长度大于系统支持的长度’无法删除解决办法
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
CentOS 7.9 installing Oracle 21C Adventures
Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
High voltage leakage relay bld-20
Différenciation et introduction des services groupés, distribués et microservices
Simple case of SSM framework
AI人脸编辑让Lena微笑