当前位置:网站首页>【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;
}
};
边栏推荐
- 项目场景 with ERRTYPE = cudaError CUDA failure 999 unknown error
- IPFS deployment and file upload (golang)
- Lombok
- 灰度传感器、、、diy原理。。图
- 永磁同步电机36问(三)——SVPWM代码实现
- 【LeetCode】20.有效的括号
- Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
- Install mysql using docker
- svm.SVC应用实践1--乳腺癌检测
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
猜你喜欢

局部敏感哈希:如何在常数时间内搜索Embedding最近邻

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

Oracle数据类型介绍

FOFAHUB usage test

Flask入门学习教程
![[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

aws s3 upload file

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

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

svm.SVC应用实践1--乳腺癌检测
随机推荐
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
- daily a LeetCode 】 【 9. Palindrome
mysql 查看死锁
树链剖分-
PAT甲级打卡-1001-1004
微信小程序异步回调函数恶梦和解决办法
架构:应用架构的演进以及微服务架构的落地实践
Chrome浏览器无法加载已解压的.crx文件的解决办法
【LeetCode】144.二叉树的前序遍历
cadence landscape bindkey
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
第10章_索引优化与查询优化
IPFS部署及文件上传(golang)
esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
analog IC layout
【LeetCode】145.二叉树的后序遍历
AcWing 1285. Word Problem Solving (AC Automata)
一次SQL优化,数据库查询速度提升 60 倍
NAS和私有云盘的区别?1篇文章说清楚