当前位置:网站首页>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;
}
};
边栏推荐
- “空间转换”显著提升陡崖点云的地面点提取质量
- OAuth 2.0 one click login
- 27. 移除元素
- How much do you know about electronic components on PCB?
- Manufacturing process of PCB 2021-10-11
- VOCALOID笔记
- Collection of common terms and meanings in forestry investigation based on lidar
- Technology blog | how to communicate using SSE
- Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
- Analysis of kinsing dual platform mining family virus
猜你喜欢
Keil and Proteus joint commissioning
NPM install reports an error: gyp err! configure error
Six causes of PCB disconnection 2021-10-20
Tips on how to design soft and hard composite boards ~ 22021/11/22
Technology blog | how to communicate using SSE
【QT】Qt 5 的程序:打印文档
Summary of small problems in smartbugs installation
VSCode很好,但我以后不会再用了
传统的IO存在什么问题?为什么引入零拷贝的?
"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
随机推荐
@Resource和@Autowired注解的不同,为什么推荐@Resource?
opencv最小值滤波(不局限于图像)
判断用户是否是第一次进入某个页面
Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute
产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
Summary of small problems in smartbugs installation
VSCode很好,但我以后不会再用了
Analysis of kinsing dual platform mining family virus
2160. minimum sum of the last four digits after splitting
Different paths ii[dynamic planning improvement for DFS]
Do you know why the PCB produces tin beads? 2021-09-30
Accès à la boîte aux lettres du nom de domaine Lead à l'étranger
Buckle 78: subset
Modular programming of LCD1602 LCD controlled by single chip microcomputer
搞清信息化是什么,让企业转型升级走上正确的道路
【视频】ffplay 使用mjpeg格式播放usb摄像头
(tool class) use SecureCRT as the communication medium
Force deduction 76 questions, minimum covering string
npm install 报错 : gyp ERR! configure error
Usememo simulation usecallback