429. N 叉树的层序遍历
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/56d805433f8f8c01f1fbe358759af9ca.png)
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;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7d563f0254d82343e8e5aebdc044bfe8.png)
515.在每个树行中找最大值
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e5b40e3a84b6aa369a759a661750c090.png)
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;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3da6389c0cdacabd41fcd6267c7b68a0.png)
116.填充每个节点的下一个右侧节点指针
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f293e24d76445b9b03a47f6b1bf499b4.png)
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root != NULL) que.push(root);
while (!que.empty()) {
int size = que.size();
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;
nodePre = nodePre->next;
}
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
nodePre->next = NULL;
}
return root;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7fbce2a4ffd921be41d6faf149d0059a.png)
104.二叉树的最大深度
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1dea4308ac87787a34874b13d1600fc0.png)
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;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3f2a2e429b9028eb62021bd9dd588ab9.png)
111.二叉树的最小深度
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3b01c4ed2c7c99549713db3598bddc89.png)
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;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cfa9b69bdb28203b939a63081df94592.png)