当前位置:网站首页>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;
}
};
边栏推荐
- What are the problems with traditional IO? Why is zero copy introduced?
- 27. 移除元素
- This article uses pytorch to build Gan model!
- php入门基础记录
- How to use ad wiring for PCB design?
- CAN总线工作状况和信号质量“体检”
- Atlas conference vulnerability analysis collection
- Tips 𞓜 how to clean PCB boards 2021-10-22
- Storage of Galileo broadcast ephemeris in rtklib-b33
- Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134 vulnerability analysis and protection
猜你喜欢
navicat定时任务无效
C#中如何调整图像大小
机器学习笔记 - 时间序列的线性回归
Technology blog | how to communicate using SSE
Tips on how to design soft and hard composite boards ~ 22021/11/22
How to use printf of 51 single chip microcomputer
权限、认证系统相关名词概念
How to resize an image in C #
Application of point cloud intelligent drawing in intelligent construction site
环网冗余式CAN/光纤转换器的CAN光端机在消防火灾联网报警系统中的应用
随机推荐
Mysql面试-执行sql响应比较慢,排查思路。
Modular programming of oled12864 display controlled by single chip microcomputer
MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
Runtime——methods成员变量,cache成员变量
Force deduction 76 questions, minimum covering string
Technology blog | how to communicate using SSE
NSIS 静默安装vs2013运行时
如何用svn新建属于自己的分支
Keil and Proteus joint commissioning
将数据导入到MATLAB
Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
Linux上oracle和mysql的启动,关闭,重启
Estimation of dense forest volume based on LIDAR point cloud with few ground points
Pit encountered by pytorch: why can't l1loss decrease during model training?
c# winform panel自定义图片和文字
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
C reads XML on the web
[deep learning lightweight backbone] 2022 edgevits CVPR
基于RBAC 的SAAS系统权限设计