当前位置:网站首页>力扣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;
}
边栏推荐
- Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
- 论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】
- JVM (XX) -- performance monitoring and tuning (I) -- Overview
- Intelligent annotation scheme of entity recognition based on hugging Face Pre training model: generate doccano request JSON format
- DOM node object + time node comprehensive case
- [JS component] custom select
- Leakage relay jd1-100
- 一条 update 语句的生命经历
- How does mapbox switch markup languages?
- [Oracle] simple date and time formatting and sorting problem
猜你喜欢
判断文件是否为DICOM文件
sql查询:将下一行减去上一行,并做相应的计算
The 2022 China low / no code Market Research and model selection evaluation report was released
论文阅读【Semantic Tag Augmented XlanV Model for Video Captioning】
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
基于NCF的多模块协同实例
High voltage leakage relay bld-20
K6el-100 leakage relay
Digital innovation driven guide
[JS component] custom select
随机推荐
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
Mybaits之多表查询(联合查询、嵌套查询)
C#可空类型
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
分布式全局ID生成方案
What is message queuing?
Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
张平安:加快云上数字创新,共建产业智慧生态
Educational Codeforces Round 22 B. The Golden Age
[paper reading] semi supervised left atrium segmentation with mutual consistency training
Mybaits multi table query (joint query, nested query)
Educational Codeforces Round 22 B. The Golden Age
Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred
5. 数据访问 - EntityFramework集成
判断文件是否为DICOM文件
What are the common message queues?
Five core elements of architecture design
When deleting a file, the prompt "the length of the source file name is greater than the length supported by the system" cannot be deleted. Solution
Distributed global ID generation scheme
Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding