关于层序遍历的九道题
代码随想录算法训练营第1天|704. 二分查找、27. 移除元素
107. 二叉树的层序遍历II
提交代码
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
queue<TreeNode*> que;
if(root == nullptr)
return result;
que.push(root);
while(!que.empty())
{
int size = que.size();
vector<int> vec;
while(size--)
{
TreeNode* node = que.front();
que.pop();
vec.push_back(node -> val);
if(node -> left != nullptr)
que.push(node -> left);
if(node -> right != nullptr)
que.push(node -> right);
}
result.push_back(vec);
}
reverse(result.begin(), result.end());
return result;
}
};
199.二叉树的右视图
提交代码(方法)
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
queue<TreeNode*> que;
if(root == nullptr)
return result;
que.push(root);
while(!que.empty())
{
int size = que.size();
while(size--)
{
TreeNode* node = que.front();
que.pop();
if(size == 0)
result.push_back(node -> val);
if(node -> left != nullptr)
que.push(node -> left);
if(node -> right != nullptr)
que.push(node -> right);
}
}
//reverse(result.begin(), result.end());
return result;
}
};
637.二叉树的层平均值
提交代码(方法)
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> result;
queue<TreeNode*> que;
if(root == nullptr)
return result;
que.push(root);
while(!que.empty())
{
int size = que.size();
vector<int> vec;
while(size--)
{
TreeNode* node = que.front();
que.pop();
vec.push_back(node -> val);
if(node -> left != nullptr)
que.push(node -> left);
if(node -> right != nullptr)
que.push(node -> right);
}
double sum = 0;
for(auto num:vec)
sum += num;
double curRes = sum / (double) vec.size();
result.push_back(curRes);
}
return result;
}
};
429.N叉树的层序遍历
提交代码(方法)
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> result;
queue<vector<Node*>> que;
if(root == nullptr)
return result;
que.push(vector<Node*>{root});
while(!que.empty())
{
int size = que.size();
vector<int> vec;
while(size--)
{
vector<Node*> node = que.front();
que.pop();
int sizeNode = node.size();
for(int i = 0; i <sizeNode; i++)
{
vec.push_back(node[i] -> val);
if(node[i] -> children.size() != 0)
que.push(node[i] -> children);
}
}
result.push_back(vec);
}
return result;
}
};
515.在每个树行中找最大值
提交代码(方法)
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> result;
queue<TreeNode*> que;
if(root == nullptr)
return result;
que.push(root);
while(!que.empty())
{
int size = que.size();
vector<int> vec;
while(size--)
{
TreeNode* node = que.front();
que.pop();
vec.push_back(node -> val);
if(node -> left != nullptr)
que.push(node -> left);
if(node -> right != nullptr)
que.push(node -> right);
}
int max = vec[0];
for(int i = 1; i < vec.size(); i++)
if(vec[i] >max)
max = vec[i];
result.push_back(max);
}
return result;
}
};
116.填充每个节点的下一个右侧节点指针
提交代码(方法)
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if(root == nullptr)
return root;
que.push(root);
while(!que.empty())
{
int size = que.size();
vector<int> vec;
while(size--)
{
Node* node = que.front();
que.pop();
if(size == 0)
node -> next = NULL;
else
node -> next = que.front();
if(node -> left != nullptr)
que.push(node -> left);
if(node -> right != nullptr)
que.push(node -> right);
}
}
return root;
}
};
117.填充每个节点的下一个右侧节点指针II
提交代码(方法)
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if(root == nullptr)
return root;
que.push(root);
while(!que.empty())
{
int size = que.size();
vector<int> vec;
while(size--)
{
Node* node = que.front();
que.pop();
if(size == 0)
node -> next = NULL;
else
node -> next = que.front();
if(node -> left != nullptr)
que.push(node -> left);
if(node -> right != nullptr)
que.push(node -> right);
}
}
return root;
}
};
104.二叉树的最大深度
提交代码(方法)
class Solution {
public:
int maxDepth(TreeNode* root) {
int depth = 0;
queue<TreeNode*> que;
if(root == nullptr)
return 0;
que.push(root);
while(!que.empty())
{
depth++;
int size = que.size();
while(size--)
{
TreeNode* node = que.front();
que.pop();
if(node -> left != nullptr)
que.push(node -> left);
if(node -> right != nullptr)
que.push(node -> right);
}
}
return depth;
}
};
111.二叉树的最小深度
提交代码(方法)
class Solution {
public:
int minDepth(TreeNode* root) {
int depth = 0;
queue<TreeNode*> que;
if(root == nullptr)
return depth;
que.push(root);
while(!que.empty())
{
depth++;
int size = que.size();
while(size--)
{
TreeNode* node = que.front();
que.pop();
if(node -> left != nullptr)
que.push(node -> left);
if(node -> right != nullptr)
que.push(node -> right);
if(node -> left == nullptr && node -> right == nullptr)
return depth;
}
}
return depth;
}
};
总结
日期: 2023 年 3 月 29 日
学习时长: 1 h 30 m
难度:
★
\bigstar
★
累计完成题目数量: 47
距离目标还有数量: 253
小结: