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

边栏推荐
- Electronics: Lesson 010 - Experiment 8: relay oscillator
- 电子学:第012课——实验 13:烧烤 LED
- 电子学:第013课——实验 14:可穿戴的脉冲发光体
- Electronics: Lesson 011 - experiment 10: transistor switches
- Stm32cubemx Learning (5) Input capture Experiment
- c#中设置lable控件的TextAlign属性控制文字居中的方法
- Dietary intervention reduces cancer treatment-related symptoms and toxicity
- Can bus working condition and signal quality "physical examination"
- Deep learning series 48:deepfaker
- Neural network and deep learning-3-simple example of machine learning pytorch
猜你喜欢

Electronics: Lesson 012 - Experiment 13: barbecue LED

socket问题记录

Drawing of clock dial

Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer

Electronics: Lesson 009 - Experiment 7: study relays

c#磁盘驱动器及文件夹还有文件类的操作

Modeling and fault simulation of aircraft bleed system

CVPR 2022 Oral 2D图像秒变逼真3D物体

What is SKU and SPU? What is the difference between SKU and SPU

Solving some interesting problems with recurrence of function
随机推荐
Electronics: Lesson 010 - Experiment 8: relay oscillator
TCP MIN_RTO 辩证考
Electronics: Lesson 014 - Experiment 15: intrusion alarm (Part I)
Logu P2486 [sdoi2011] coloring (tree chain + segment tree + merging of intervals on the tree)
数论模板啊
Pycharm的奇葩设定:取消注释后立马复制会带上#
电子学:第009课——实验 7:研究继电器
Machine learning notes linear regression of time series
协议和服务的区别?
Mr. Tang's lecture on operational amplifier (Lecture 7) -- Application of operational amplifier
电子学:第014课——实验 15:防入侵报警器(第一部分)
Thread + thread problem record
想转行学软件测试担心哪些问题?
电子学:第011课——实验 10:晶体管开关
函数尽量不要通过变量指定操作类型
洛谷P6822 [PA2012]Tax(最短路+边变点)
[Mobius inversion]
洛谷P5994 [PA2014]Kuglarz(异或思维+MST)
FFT【模板】
Force buckle 272 Closest binary search tree value II recursion