当前位置:网站首页>【LeetCode】102.二叉树的层序遍历
【LeetCode】102.二叉树的层序遍历
2022-08-02 02:40:00 【酥酥~】
题目
给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
提示:
树中节点数目在范围 [0, 2000] 内
-1000 <= Node.val <= 1000
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(root==nullptr)
return {
};
queue<TreeNode*> myqueue;
myqueue.emplace(root);
vector<vector<int>> result;
while(!myqueue.empty())
{
result.emplace_back(vector<int>());
int len = myqueue.size();
for(int i=0;i<len;i++)
{
TreeNode* node = myqueue.front();
result.back().emplace_back(node->val);
myqueue.pop();
if(node->left)
myqueue.emplace(node->left);
if(node->right)
myqueue.emplace(node->right);
}
}
return result;
}
};
边栏推荐
- 2022-08-01 mysql/stoonedb慢SQL-Q18分析
- 亲身经历过的面试题
- [Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
- AWR analysis report questions for help: How can SQL be optimized from what aspects?
- 【 wheeled odometer 】
- 数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
- Swift运行时(派发机制)
- Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
- C#测试项目中属性的用法
- 第11章_数据库的设计规范
猜你喜欢

Install mysql using docker

详解最强分布式锁工具:Redisson
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games

Unable to log in to the Westward Journey

2022-08-01 mysql/stoonedb慢SQL-Q18分析

aws s3上传文件

Oracle19c安装图文教程

记一次gorm事务及调试解决mysql死锁

Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided

【web】Understanding Cookie and Session Mechanism
随机推荐
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
Lombok
Power button 1374. Generate each character string is an odd number
内卷的正确打开方式
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
架构:分布式任务调度系统(SIA-Task)简介
What to study after the PMP exam?The soft exam ahead is waiting for you~
BI - SQL 丨 WHILE
mysql 查看死锁
IMU预积分的简单理解
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
使用self和_(下划线)的区别
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
2022牛客多校三_F G
Ringtone 1161. Maximum In-Layer Elements and
A good book for newcomers to the workplace
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
KICAD 小封装拉线卡顿问题 解决方法