当前位置:网站首页>【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;
}
};
边栏推荐
- [Daily LeetCode]——1. The sum of two numbers
- BI-SQL丨WHILE
- Flask 报错:WARNING This is a development server. Do not use it in a production deployment
- 字符串常用方法
- 790. 数的三次方根
- 工程师如何对待开源
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
- 搭建zabbix监控及邮件报警(超详细教学)
- 1688以图搜货
- 罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
猜你喜欢
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
Flask之路由(app.route)详解
Flask入门学习教程
Flask 报错:WARNING This is a development server. Do not use it in a production deployment
AI target segmentation capability for fast video cutout without green screen
树链剖分-
The failure to create a role in Dahua Westward Journey has been solved
面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
ros多客户端请求服务
随机推荐
Docker-compose安装mysql
yaml
Curriculum Vitae;CV
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
Oracle数据类型介绍
周鸿祎称微软抄袭,窃取360安全模式
JVM调优实战
【LeetCode】144.二叉树的前序遍历
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
Chapter 7 Noise analysis
Flask 报错:WARNING This is a development server. Do not use it in a production deployment
第10章_索引优化与查询优化
考完PMP学什么?前方软考等着你~
微信小程序异步回调函数恶梦和解决办法
How ReentrantLock works
GTK RGB图像绘制
OperatingSystemMXBean获取系统性能指标
Nacos源码分析专题(二)-服务注册
(一)Redis: 基于 Key-Value 的存储系统
架构:应用架构的演进以及微服务架构的落地实践