当前位置:网站首页>力扣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;
}
边栏推荐
- 论文阅读【MM21 Pre-training for Video Understanding Challenge:Video Captioning with Pretraining Techniqu】
- The navigation bar changes colors according to the route
- Educational Codeforces Round 22 B. The Golden Age
- Mybaits multi table query (joint query, nested query)
- 5. Data access - entityframework integration
- Flink SQL 实现读写redis,并动态生成Hset key
- Lombok plug-in
- If you want to choose some departments to give priority to OKR, how should you choose pilot departments?
- Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
- What is dependency injection (DI)
猜你喜欢

Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting

How Alibaba cloud's DPCA architecture works | popular science diagram

AI人脸编辑让Lena微笑

5. 数据访问 - EntityFramework集成

SAP ABAP BDC(批量数据通信)-018

Message queue: how to deal with message backlog?

TCC of distributed transaction solutions

Getting started with DES encryption

Message queuing: how to ensure that messages are not lost

How does mapbox switch markup languages?
随机推荐
SAP ABAP BDC(批量数据通信)-018
Mysql database learning (7) -- a brief introduction to pymysql
1. AVL tree: left-right rotation -bite
分布式事务解决方案之2PC
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation
Batch size setting skills
拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
App clear data source code tracking
win配置pm2开机自启node项目
说一说MVCC多版本并发控制器?
How digitalization affects workflow automation
Paper reading [semantic tag enlarged xlnv model for video captioning]
The 2022 China low / no code Market Research and model selection evaluation report was released
得物客服一站式工作台卡顿优化之路
English语法_名词 - 所有格
消息队列:如何确保消息不会丢失
删除文件时提示‘源文件名长度大于系统支持的长度’无法删除解决办法
Talk about mvcc multi version concurrency controller?
[Oracle] simple date and time formatting and sorting problem
微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位