当前位置:网站首页>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 p2048 [noi2010] super Piano (rmq+ priority queue)
- c#中设置lable控件的TextAlign属性控制文字居中的方法
- 27. remove elements
- [deep learning lightweight backbone] 2022 edgevits CVPR
- Debugging mipi-dsi screen based on stm32mp157
- 六月集训(第25天) —— 树状数组
- CVPR 2022 Oral 2D图像秒变逼真3D物体
- 洛谷P5994 [PA2014]Kuglarz(异或思维+MST)
- [red flag Cup] Supplementary questions
- Neural network and deep learning-3-simple example of machine learning pytorch
猜你喜欢

Electronics: Lesson 010 - Experiment 9: time and capacitors

TCP的那点玩意儿

Socket problem record

Stm32cubemx learning (5) input capture experiment

Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus

Apache CouchDB 代码执行漏洞(CVE-2022-24706 )批量POC

Overview of image super score: the past and present life of image super score in a single screen (with core code)

电子学:第008课——实验 6:非常简单的开关

Sword finger offer (medium level)

Stm32cubemx Learning (5) Input capture Experiment
随机推荐
TCP与UDP
Opencv minimum filtering (not limited to images)
Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line
想转行学软件测试担心哪些问题?
Determine whether the user is entering a page for the first time
飞机引气系统的建模与故障仿真
現在通過開戶經理發的開戶鏈接股票開戶安全嗎?
Pychart's wonderful setting: copy immediately after canceling the comment and bring #
Sword finger offer (simple level)
Luogu p6822 [pa2012]tax (shortest circuit + edge change point)
取消word文档中某些页面的页眉
静态网页服务器
Luogu p1073 [noip2009 improvement group] optimal trade (layered diagram + shortest path)
50. pow (x, n) - fast power
力扣 272. 最接近的二叉搜索树值 II 递归
MySQL simple permission management
Apache CouchDB Code Execution Vulnerability (cve-2022-24706) batch POC
27. remove elements
电子学:第012课——实验 13:烧烤 LED
C # set up FTP server and realize file uploading and downloading