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

边栏推荐
- How much do you know about electronic components on PCB?
- C Getting Started tutorial
- Lebel only wants an asterisk in front of it, but doesn't want to verify it
- 如何用svn新建属于自己的分支
- 【QT】Qt 5 的程序:打印文档
- Runtime——methods成员变量,cache成员变量
- WinForm implementation window is always at the top level
- Atlas conference vulnerability analysis collection
- Requirements for Power PCB circuit board design 2021-11-09
- Share the process requirements for single-layer flexible circuit board
猜你喜欢

基于STM32MP157调试MIPI-DSI屏幕

机器学习笔记 - 时间序列的线性回归

一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药

基于地面点稀少的LiDAR点云的茂密森林蓄积量估算

SCM Project Training

Six causes of PCB disconnection 2021-10-20

基于Anaconda的模块安装与注意事项
![Insert and sort the linked list [dummy unified operation + broken chain core - passive node]](/img/2a/ccb1145d2b4f9fbd8d0812deace93b.png)
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]

点云智绘在智慧工地中的应用

传统的IO存在什么问题?为什么引入零拷贝的?
随机推荐
[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
Audio (V) audio feature extraction
What are the benefits of reserving process edges for PCB production? 2021-10-25
Force deduction 76 questions, minimum covering string
Kinsing双平台挖矿家族病毒分析
权限、认证系统相关名词概念
Atlas conference vulnerability analysis collection
(tool class) use SecureCRT as the communication medium
What are the problems with traditional IO? Why is zero copy introduced?
基于STM32MP157调试MIPI-DSI屏幕
C reads XML on the web
VOCALOID笔记
How to resize an image in C #
海思3559 sample解析:vio
CAN总线工作状况和信号质量“体检”
@The difference between resource and @autowired annotation, why recommend @resource?
NSIS silent installation vs2013 runtime
Mysql面试-执行sql响应比较慢,排查思路。
C Getting Started tutorial
Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用