当前位置:网站首页>【LeetCode】102. Level order traversal of binary tree
【LeetCode】102. Level order traversal of binary tree
2022-08-02 02:46:00 【Crispy~】
题目
给你二叉树的根节点 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;
}
};
边栏推荐
- 启发式合并、DSU on Tree
- VPS8701 电源管理(PMIC) VPS8701
- Oracle19c安装图文教程
- [LeetCode] 94. Inorder traversal of binary tree
- canal同步Mariadb到Mysql
- How ReentrantLock works
- Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
- AcWing 1285. Word Problem Solving (AC Automata)
- cocos中使用async await异步加载资源
- TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
猜你喜欢

四元数、罗德里格斯公式、欧拉角、旋转矩阵推导和资料

永磁同步电机36问(二)——机械量与电物理量如何转化?

Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol

2022牛客多校三_F G

很有意思的经历,很有意思的项目--文件夹对比工具

BI - SQL 丨 WHILE

Flask之路由(app.route)详解

Use DBeaver for mysql data backup and recovery

Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles

Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
随机推荐
2022牛客多校三_F G
Nacos源码分析专题(一)-环境准备
亲身经历过的面试题
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
yaml
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
剑指 Offer 14- I. 剪绳子
第10章_索引优化与查询优化
node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module ‘./scope‘
Qt自定义控件和模板分享
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
PAT甲级打卡-1001-1004
【LeetCode】144.二叉树的前序遍历
Docker-compose安装mysql
忽晴忽雨
Install mysql using docker
NAS和私有云盘的区别?1篇文章说清楚
analog IC layout-Design for reliability
51. 数字排列