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

边栏推荐
- NSIS 静默安装vs2013运行时
- "Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
- How much do you know about electronic components on PCB?
- 基于STM32MP157调试MIPI-DSI屏幕
- 【日常训练】207. 课程表
- PCB board design - automatic layout 2021-10-15
- Anaconda navigator启动慢的一个解决方法
- 双三次差值bicubic
- 神经网络与深度学习-3- 机器学习简单示例-PyTorch
- CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据
猜你喜欢

Basic use of ActiveMQ in Message Oriented Middleware

新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
![[deep learning lightweight backbone] 2022 edgevits CVPR](/img/13/139d28621403020e3475a30f6148f8.png)
[deep learning lightweight backbone] 2022 edgevits CVPR

传统的IO存在什么问题?为什么引入零拷贝的?

海思3559 sample解析:vio

SCM Project Training

PCB board design - automatic layout 2021-10-15

Four software 2021-10-14 suitable for beginners to draw PCB

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

OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
随机推荐
Tips 𞓜 how to clean PCB boards 2021-10-22
50. pow (x, n) - fast power
Pit encountered by pytorch: why can't l1loss decrease during model training?
The method of judging whether triode can amplify AC signal
微信小程序开通客服消息功能开发
WinForm implementation window is always at the top level
【QT】qtcreator便捷快捷键以及QML介绍
Tips on how to design soft and hard composite boards ~ 22021/11/22
Requirements for Power PCB circuit board design 2021-11-09
C#中如何调整图像大小
el-input实现尾部加字
Storage of Galileo broadcast ephemeris in rtklib-b33
STL tutorial 4- input / output stream and object serialization
1742. maximum number of small balls in the box
如何用svn新建属于自己的分支
CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据
test
C reads XML on the web
Runtime - Methods member variable, cache member variable
Fairmot yolov5s to onnx