当前位置:网站首页>417-二叉树的层序遍历1(102. 二叉树的层序遍历、107.二叉树的层次遍历 II、199.二叉树的右视图、637.二叉树的层平均值)
417-二叉树的层序遍历1(102. 二叉树的层序遍历、107.二叉树的层次遍历 II、199.二叉树的右视图、637.二叉树的层平均值)
2022-06-25 06:42:00 【liufeng2023】
102. 二叉树的层序遍历

class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != nullptr) que.push(root);
vector<vector<int>> res;
while (!que.empty())
{
int size = que.size();
vector<int> temp;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
temp.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
res.push_back(temp);
}
return res;
}
};

107.二叉树的层次遍历 II

class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
queue<TreeNode*> que;
vector<vector<int>> res;
if (root != nullptr) que.push(root);
while (!que.empty())
{
int size = que.size();
vector<int> temp;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
temp.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
res.push_back(temp);
}
reverse(res.begin(), res.end());
return res;
}
};

199.二叉树的右视图

class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<int> result;
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (i == (size - 1)) result.push_back(node->val); // 将每一层的最后元素放入result数组中
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return result;
}
};

637.二叉树的层平均值

class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<double> result;
while (!que.empty()) {
int size = que.size();
double sum = 0; // 统计每一层的和
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
sum += node->val;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(sum / size); // 将每一层均值放进结果集
}
return result;
}
};

边栏推荐
- 权限、认证系统相关名词概念
- 【QT】qtcreator便捷快捷键以及QML介绍
- opencv最小值滤波(不局限于图像)
- Misunderstanding of switching triode
- Runtime - Methods member variable, cache member variable
- Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
- Linux上oracle和mysql的启动,关闭,重启
- CAN总线工作状况和信号质量“体检”
- Estimation of dense forest volume based on LIDAR point cloud with few ground points
- C#控件刷新
猜你喜欢
![[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905](/img/a7/fc5d2f4640322a5d7222cce83c8898.jpg)
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905

【论文学习】《VQMIVC》
![[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection](/img/5c/ad42474a363c33ecc0e01890b65bbf.png)
[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection

Five causes of PCB board deformation and six solutions 2021-10-08

Pcb|about FPC reinforcement type

VOCALOID笔记

"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud

一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局

饮食干预减轻癌症治疗相关症状和毒性

取消word文档中某些页面的页眉
随机推荐
@Resource和@Autowired注解的不同,为什么推荐@Resource?
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
基于STM32MP157调试MIPI-DSI屏幕
使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障
MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
2160. minimum sum of the last four digits after splitting
npm install 报错 : gyp ERR! configure error
1742. 盒子中小球的最大数量
Six causes of PCB disconnection 2021-10-20
Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
JDBC-DAO层实现
Collection of common terms and meanings in forestry investigation based on lidar
Bicubic difference
How to use ad wiring for PCB design?
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
2160. 拆分数位后四位数字的最小和
Five causes of PCB board deformation and six solutions 2021-10-08
剑指 Offer II 027. 回文链表
ts环境搭建