当前位置:网站首页>420 sequence traversal of binary tree 2 (429. sequence traversal of n-ary tree, 515. find the maximum value in each tree row, 116. fill in the next right node pointer of each node, 104. maximum depth
420 sequence traversal of binary tree 2 (429. sequence traversal of n-ary tree, 515. find the maximum value in each tree row, 116. fill in the next right node pointer of each node, 104. maximum depth
2022-06-25 08:10:00 【liufeng2023】
429. N Sequence traversal of the fork tree
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. Find the maximum in each tree row
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. Fill in the next right node pointer for each node
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(); // Take out the head node of the first layer
que.pop();
node = nodePre;
} else {
node = que.front();
que.pop();
nodePre->next = node; // Previous node of this layer next Point to this node
nodePre = nodePre->next;
}
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
nodePre->next = NULL; // The last node of this layer points to NULL
}
return root;
}
};
104. The maximum depth of a binary tree
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. Minimum depth of binary tree
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;
}
};
边栏推荐
- Luogu p2839 [national training team]middle (two points + chairman tree + interval merging)
- Electronics: Lesson 012 - Experiment 13: barbecue LED
- C disk drives, folders and file operations
- Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely
- Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer
- [deep learning lightweight backbone] 2022 edgevits CVPR
- c#磁盘驱动器及文件夹还有文件类的操作
- Cloud computing exam version 1 0
- 將數據導入到MATLAB
- Can bus working condition and signal quality "physical examination"
猜你喜欢
Modeling and fault simulation of aircraft bleed system
深度学习系列48:DeepFaker
Importer des données dans MATLAB
C disk drives, folders and file operations
静态网页服务器
TCP与UDP
Network model -- OSI model and tcp/ip model
To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity
socket问题记录
Authority design of SaaS system based on RBAC
随机推荐
Solving some interesting problems with recurrence of function
The first game of 2021 ICPC online game
力扣 272. 最接近的二叉搜索树值 II 递归
What is SKU and SPU? What is the difference between SKU and SPU
TCP MIN_RTO 辩证考
Can bus working condition and signal quality "physical examination"
电子学:第009课——实验 7:研究继电器
电子学:第010课——实验 9:时间与电容器
Is it safe to open an account through the haircut account opening link now?
云计算考试版本1.0
电子学:第008课——实验 6:非常简单的开关
Neural network and deep learning-3-simple example of machine learning pytorch
【莫比乌斯反演】
[supplementary question] 2021 Niuke summer multi school training camp 6-n
To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity
c#磁盘驱动器及文件夹还有文件类的操作
牛客:飞行路线(分层图+最短路)
Electronics: Lesson 009 - Experiment 7: study relays
Luogu p2839 [national training team]middle (two points + chairman tree + interval merging)
取消word文档中某些页面的页眉