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

边栏推荐
- 如何用svn新建属于自己的分支
- Collection of common terms and meanings in forestry investigation based on lidar
- VSCode很好,但我以后不会再用了
- 用函数的递归来解决几道有趣的题
- Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
- Estimation of dense forest volume based on LIDAR point cloud with few ground points
- How to use printf of 51 single chip microcomputer
- 27. 移除元素
- WinForm实现窗口始终在顶层
- 传统的IO存在什么问题?为什么引入零拷贝的?
猜你喜欢

Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading

FairMOT yolov5s转onnx

Anaconda navigator启动慢的一个解决方法

Do you know why the PCB produces tin beads? 2021-09-30

OAuth 2.0 one click login

基于Anaconda的模块安装与注意事项

OAuth 2.0一键登录那些事

三台西门子消防主机FC18配套CAN光端机进行光纤冗余环网组网测试

How to use ad wiring for PCB design?
![[deep learning lightweight backbone] 2022 edgevits CVPR](/img/13/139d28621403020e3475a30f6148f8.png)
[deep learning lightweight backbone] 2022 edgevits CVPR
随机推荐
@Resource和@Autowired注解的不同,为什么推荐@Resource?
环网冗余式CAN/光纤转换器的CAN光端机在消防火灾联网报警系统中的应用
2265. 统计值等于子树平均值的节点数
力扣76题,最小覆盖字串
Application of point cloud intelligent drawing in intelligent construction site
50. pow (x, n) - fast power
Basic use of ActiveMQ in Message Oriented Middleware
Kinsing双平台挖矿家族病毒分析
JDBC-DAO层实现
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
C#获取exe的版本号-文件版本and程序集版本
点云智绘在智慧工地中的应用
[daily training] 207 Class Schedule Card
Force deduction 76 questions, minimum covering string
Getting started with OpenMP
Understand the reasons for impedance matching of PCB circuit board 2021-10-07
力扣76题,最小覆盖字串
Six causes of PCB disconnection 2021-10-20
双三次差值bicubic
2160. minimum sum of the last four digits after splitting