当前位置:网站首页>力扣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;
}
边栏推荐
- The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
- 5. Data access - entityframework integration
- Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding
- Educational Codeforces Round 22 B. The Golden Age
- 删除文件时提示‘源文件名长度大于系统支持的长度’无法删除解决办法
- Mysql database learning (8) -- MySQL content supplement
- MySQL-CentOS7通过YUM安装MySQL
- [paper reading] semi supervised left atrium segmentation with mutual consistency training
- 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
- Paper reading [open book video captioning with retrieve copy generate network]
猜你喜欢
Unity让摄像机一直跟随在玩家后上方
What is message queuing?
R语言【逻辑控制】【数学运算】
JVM (19) -- bytecode and class loading (4) -- talk about class loader again
JVM (XX) -- performance monitoring and tuning (I) -- Overview
LabVIEW is opening a new reference, indicating that the memory is full
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
论文阅读【MM21 Pre-training for Video Understanding Challenge:Video Captioning with Pretraining Techniqu】
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation
2pc of distributed transaction solution
随机推荐
async / await
随机生成session_id
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
Jhok-zbl1 leakage relay
DOM node object + time node comprehensive case
Message queuing: how to ensure that messages are not lost
基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
How does mapbox switch markup languages?
Leetcode 1189 maximum number of "balloons" [map] the leetcode road of heroding
[JS component] date display.
Mybaits之多表查询(联合查询、嵌套查询)
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
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation
Egr-20uscm ground fault relay
"Multimodal" concept
The 2022 China low / no code Market Research and model selection evaluation report was released
MySQL数据库学习(8) -- mysql 内容补充
爬虫练习题(三)
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
[binary tree] binary tree path finding