当前位置:网站首页>420-二叉树的层序遍历2(429. N 叉树的层序遍历、515.在每个树行中找最大值、116.填充每个节点的下一个右侧节点指针、104.二叉树的最大深度、111.二叉树的最小深度)
420-二叉树的层序遍历2(429. N 叉树的层序遍历、515.在每个树行中找最大值、116.填充每个节点的下一个右侧节点指针、104.二叉树的最大深度、111.二叉树的最小深度)
2022-06-25 06:42:00 【liufeng2023】
429. N 叉树的层序遍历

class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> res;
queue<Node*> que;
if (root != nullptr) que.push(root);
while (!que.empty())
{
vector<int> temp;
int size = que.size();
for (int i = 0; i < size; i++)
{
Node* node = que.front();
que.pop();
temp.push_back(node->val);
for (int i = 0; i < (node->children.size()); i++)
{
if (node->children[i]) que.push(node->children[i]);
}
}
res.push_back(temp);
}
return res;
}
};

515.在每个树行中找最大值

class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> res;
queue<TreeNode*> que;
if (root != nullptr) que.push(root);
while (!que.empty())
{
int size = que.size();
int temp = INT32_MIN;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
temp = (node->val > temp) ? node->val : temp;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
res.push_back(temp);
}
return res;
}
};

116.填充每个节点的下一个右侧节点指针

class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root != NULL) que.push(root);
while (!que.empty()) {
int size = que.size();
// vector<int> vec;
Node* nodePre;
Node* node;
for (int i = 0; i < size; i++) {
if (i == 0) {
nodePre = que.front(); // 取出一层的头结点
que.pop();
node = nodePre;
} else {
node = que.front();
que.pop();
nodePre->next = node; // 本层前一个节点next指向本节点
nodePre = nodePre->next;
}
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
nodePre->next = NULL; // 本层最后一个节点指向NULL
}
return root;
}
};

104.二叉树的最大深度

class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*> que;
int res = 0;
if (root) que.push(root);
while (!que.empty())
{
int size = que.size();
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
res++;
}
return res;
}
};

111.二叉树的最小深度

class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
int res = 0;
if (root) que.push(root);
while (!que.empty())
{
int size = que.size();
res++;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
if (node->left == nullptr && node->right == nullptr) return res;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return res;
}
};

边栏推荐
- How to use ad wiring for PCB design?
- C control refresh
- 2160. minimum sum of the last four digits after splitting
- 搞清信息化是什么,让企业转型升级走上正确的道路
- opencv最小值滤波(不局限于图像)
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
- C#控件刷新
- C get the version number of exe - file version and assembly version
- php入门基础记录
- "Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
猜你喜欢

饮食干预减轻癌症治疗相关症状和毒性

opencv最小值滤波(不局限于图像)

How to resize an image in C #

将数据导入到MATLAB

环网冗余式CAN/光纤转换器的CAN光端机在消防火灾联网报警系统中的应用
![[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905](/img/a7/fc5d2f4640322a5d7222cce83c8898.jpg)
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905

“空间转换”显著提升陡崖点云的地面点提取质量

Summary of small problems in smartbugs installation

Technology blog | how to communicate using SSE

【QT】Qt 5 的程序:打印文档
随机推荐
2160. minimum sum of the last four digits after splitting
How to use ad wiring for PCB design?
C control refresh
Requirements for Power PCB circuit board design 2021-11-09
Understand the reasons for impedance matching of PCB circuit board 2021-10-07
如何用svn新建属于自己的分支
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
OAuth 2.0 one click login
Kinsing双平台挖矿家族病毒分析
Anaconda navigator启动慢的一个解决方法
Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障
Analysis of kinsing dual platform mining family virus
基于RBAC 的SAAS系统权限设计
50. Pow(x, n)-快速幂
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
C#获取exe的版本号-文件版本and程序集版本
VOCALOID笔记
Tips 𞓜 how to clean PCB boards 2021-10-22
传统的IO存在什么问题?为什么引入零拷贝的?