当前位置:网站首页>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;
}
};

边栏推荐
- 力扣76题,最小覆盖字串
- STL tutorial 4- input / output stream and object serialization
- Linux上oracle和mysql的启动,关闭,重启
- Atlas conference vulnerability analysis collection
- 基于RBAC 的SAAS系统权限设计
- How to use ad wiring for PCB design?
- Runtime——methods成员变量,cache成员变量
- Vscode is good, but I won't use it again
- WinForm实现窗口始终在顶层
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
猜你喜欢

C#中如何调整图像大小

SCM Project Training

Sword finger offer II 027 Palindrome linked list

Requirements for Power PCB circuit board design 2021-11-09

新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍

How to use ad wiring for PCB design?

使用Adobe Acrobat Pro调整PDF页面为统一大小

El input to add words to the tail

年后求职找B端产品经理?差点把自己坑惨了......

基于STM32MP157调试MIPI-DSI屏幕
随机推荐
基于STM32MP157调试MIPI-DSI屏幕
Basic use of ActiveMQ in Message Oriented Middleware
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
50. Pow(x, n)-快速幂
[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
点云智绘在智慧工地中的应用
STL tutorial 4- input / output stream and object serialization
Five causes of PCB board deformation and six solutions 2021-10-08
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
The fourth floor is originally the fourth floor. Let's have a look
2265. 统计值等于子树平均值的节点数
1464. 数组中两元素的最大乘积
Advantages and differences of three kinds of vias in PCB 2021-10-27
双三次差值bicubic
2160. minimum sum of the last four digits after splitting
Understand the reasons for impedance matching of PCB circuit board 2021-10-07
CAN总线工作状况和信号质量“体检”
Runtime - Methods member variable, cache member variable
搞清信息化是什么,让企业转型升级走上正确的道路