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

边栏推荐
- 判断用户是否是第一次进入某个页面
- "Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
- Usememo simulation usecallback
- C get the version number of exe - file version and assembly version
- [little knowledge] PCB proofing process
- c# winform panel自定义图片和文字
- Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
- Invalid Navicat scheduled task
- MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
- 饮食干预减轻癌症治疗相关症状和毒性
猜你喜欢

C#中如何调整图像大小

How to use printf of 51 single chip microcomputer

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

Estimation of dense forest volume based on LIDAR point cloud with few ground points

Misunderstanding of switching triode

Keil and Proteus joint commissioning

realsense d455 semantic_ Slam implements semantic octree mapping
![[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

Function template_ Class template
![[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection](/img/5c/ad42474a363c33ecc0e01890b65bbf.png)
[distillation] pointdistiller: structured knowledge distillationwards efficient and compact 3D detection
随机推荐
LeetCode_哈希表_中等_454.四数相加 II
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
Modular programming of oled12864 display controlled by single chip microcomputer
Usememo simulation usecallback
Audio (V) audio feature extraction
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
Fairmot yolov5s to onnx
点云智绘在智慧工地中的应用
Summary of small problems in smartbugs installation
navicat定时任务无效
1742. 盒子中小球的最大数量
Keil and Proteus joint commissioning
C Getting Started tutorial
1742. maximum number of small balls in the box
Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute
Atlassian confluence漏洞分析合集
Requirements for Power PCB circuit board design 2021-11-09
CAN总线工作状况和信号质量“体检”
年后求职找B端产品经理?差点把自己坑惨了......
机器学习笔记 - 时间序列的线性回归