2023年3月9日
第二十三天补
题目链接:102. 二叉树的层序遍历
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution
{
public:
vector<vector<int>> levelOrder(TreeNode *root)
{
vector<int> level;
vector<vector<int>> res;
std::queue<TreeNode *> que;
// 我用两个变量记录当前行和下一行的节点数量
int cur_size = 0;
int next_size = 0;
if (root)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
que.pop();
level.push_back(node->val);
if (node->left)
{
que.push(node->left);
++next_size;
}
if (node->right)
{
que.push(node->right);
++next_size;
}
--cur_size;
// 如果当前节点是这一层最后一个,则更新信息
if (cur_size == 0)
{
res.push_back(level);
level.clear();
cur_size = next_size;
next_size = 0;
}
}
return res;
}
};
总结:
*
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:107. 二叉树的层序遍历 II
思路:
时间复杂度:O(n)
,空间复杂度O(1)
代码:
class Solution
{
public:
vector<vector<int>> levelOrderBottom(TreeNode *root)
{
vector<int> level;
vector<vector<int>> res;
std::queue<TreeNode *> que;
int cur_size = 0;
int next_size = 0;
if (root != nullptr)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
level.push_back(node->val);
if (node->left != nullptr)
{
que.push(node->left);
++next_size;
}
if (node->right != nullptr)
{
que.push(node->right);
++next_size;
}
que.pop();
--cur_size;
if (cur_size == 0)
{
res.push_back(level);
level.clear();
cur_size = next_size;
next_size = 0;
}
}
std::reverse(res.begin(), res.end());
return res;
}
};
总结:
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:199. 二叉树的右视图
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution
{
public:
vector<int> rightSideView(TreeNode *root)
{
vector<int> res;
std::queue<TreeNode *> que;
int cur_size = 0;
int next_size = 0;
if (root != nullptr)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
if (node->left != nullptr)
{
que.push(node->left);
++next_size;
}
if (node->right != nullptr)
{
que.push(node->right);
++next_size;
}
que.pop();
--cur_size;
if (cur_size == 0)
{
res.push_back(node->val);
cur_size = next_size;
next_size = 0;
}
}
return res;
}
};
总结:
*
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:637. 二叉树的层平均值
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution
{
public:
vector<double> averageOfLevels(TreeNode *root)
{
vector<double> res;
std::queue<TreeNode *> que;
int cur_size = 0;
int next_size = 0;
int count = 0;
double sum = 0;
if (root != nullptr)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
sum += node->val;
if (node->left != nullptr)
{
que.push(node->left);
++next_size;
}
if (node->right != nullptr)
{
que.push(node->right);
++next_size;
}
que.pop();
++count;
if (cur_size == count)
{
res.push_back(sum / count);
cur_size = next_size;
next_size = 0;
count = 0;
sum = 0;
}
}
return res;
}
};
总结:
*
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:429. N 叉树的层序遍历
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
std::queue<Node*>nodeQue;
vector<vector<int>> result;
vector<int> row;
int cur_size = 0;
int next_size = 0;
if(root){
nodeQue.push(root);
++cur_size;
}
while(!nodeQue.empty())
{
auto node = nodeQue.front();
nodeQue.pop();
row.push_back(node->val);
for(auto child: node->children)
{
if(child){
nodeQue.push(child);
++next_size;
}
}
--cur_size;
if(cur_size == 0)
{
cur_size = next_size;
next_size = 0;
result.push_back(row);
row.clear();
}
}
return result;
}
};
总结:
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:515. 在每个树行中找最大值
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> level;
vector<int> res;
std::queue<TreeNode *> que;
int cur_size = 0;
int next_size = 0;
int max = INT32_MIN;
if (root)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
que.pop();
max = node->val > max ? node->val : max;
if (node->left)
{
que.push(node->left);
++next_size;
}
if (node->right)
{
que.push(node->right);
++next_size;
}
--cur_size;
// 如果当前节点是这一层最后一个,则更新信息
if (cur_size == 0)
{
res.push_back(max);
max = INT32_MIN;
cur_size = next_size;
next_size = 0;
}
}
return res;
}
};
总结:
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:116. 填充每个节点的下一个右侧节点指针
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution {
public:
Node* connect(Node* root) {
auto result = root;
std::queue<Node *> que;
Node* nextOne = nullptr;
int cur_size = 0;
int next_size = 0;
if (root)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
que.pop();
node->next = nextOne;
nextOne = node;
// 注意顺序,先右再左
if (node->right)
{
que.push(node->right);
++next_size;
}
if (node->left)
{
que.push(node->left);
++next_size;
}
--cur_size;
// 如果当前节点是这一层最后一个,则更新信息
if (cur_size == 0)
{
nextOne = nullptr;
cur_size = next_size;
next_size = 0;
}
}
return result;
}
};
总结:
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:117. 填充每个节点的下一个右侧节点指针 II
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution {
public:
Node* connect(Node* root) {
auto result = root;
std::queue<Node *> que;
Node* nextOne = nullptr;
int cur_size = 0;
int next_size = 0;
if (root)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
que.pop();
node->next = nextOne;
nextOne = node;
// 注意顺序,先右再左
if (node->right)
{
que.push(node->right);
++next_size;
}
if (node->left)
{
que.push(node->left);
++next_size;
}
--cur_size;
// 如果当前节点是这一层最后一个,则更新信息
if (cur_size == 0)
{
nextOne = nullptr;
cur_size = next_size;
next_size = 0;
}
}
return result;
}
};
总结:
*
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:104. 二叉树的最大深度
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution {
public:
int maxDepth(TreeNode* root) {
int depth = 0;
std::queue<TreeNode *> que;
// 我用两个变量记录当前行和下一行的节点数量
int cur_size = 0;
int next_size = 0;
if (root)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
que.pop();
if (node->left)
{
que.push(node->left);
++next_size;
}
if (node->right)
{
que.push(node->right);
++next_size;
}
--cur_size;
// 如果当前节点是这一层最后一个,则更新信息
if (cur_size == 0)
{
++depth;
cur_size = next_size;
next_size = 0;
}
}
return depth;
}
};
总结:
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:111. 二叉树的最小深度
思路:
时间复杂度:O(n)
,空间复杂度O(n)
代码:
class Solution {
public:
int minDepth(TreeNode* root) {
int depth;
std::queue<TreeNode *> que;
// 我用两个变量记录当前行和下一行的节点数量
int cur_size = 0;
int next_size = 0;
if (root)
{
que.push(root);
++cur_size;
}
while (!que.empty())
{
auto node = que.front();
que.pop();
if(node->left == nullptr && node->right == nullptr) return depth+1;
if (node->left)
{
que.push(node->left);
++next_size;
}
if (node->right)
{
que.push(node->right);
++next_size;
}
--cur_size;
// 如果当前节点是这一层最后一个,则更新信息
if (cur_size == 0)
{
++depth;
cur_size = next_size;
next_size = 0;
}
}
return depth;
}
};
总结:
*
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:226. 翻转二叉树
思路:
时间复杂度:O(n)
,空间复杂度O(1)
代码:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root!=nullptr)
{
std::swap(root->left,root->right);
if(root->left)invertTree(root->left);
if(root->right)invertTree(root->right);
}
return root;
}
};
总结:
哈哈
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷
题目链接:101. 对称二叉树
思路:
时间复杂度:O(n)
,空间复杂度O(1)
代码:
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return compareNode(root->left,root->right);
}
// 判断两个节点所代表的子树是否对称
bool compareNode(TreeNode* left,TreeNode* right)
{
// 都为空的话对称
if(left==nullptr && right == nullptr)return true;
// 三种不对称的情况
else if(left == nullptr && right !=nullptr)return false;
else if(left != nullptr && right ==nullptr)return false;
// 都不为空的话
else if(left->val != right->val) return false;
else
{
// left的左子树要和right的右子树相等
// 并且
// left的右子树要和right的左子树相等
// 才能说这两个节点对称
return compareNode(left->left,right->right) && compareNode(left->right,right->left);
}
}
};
总结:
哎哎
(哈哈
> 嘿嘿
> 哎哎
> 嘤嘤
)未计
一刷