关于层序遍历的十道题
时间复杂度:O(N),空间复杂度:O(N)
第一想法:使用队列遍历每一层即可
困难:没有使用固定size,因为q.size是不断变化的
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> q;
if(root != nullptr) q.push(root);
vector<vector<int>> ans;
while(!q.empty())
{
vector<int> tmp;
//使用固定size,因为q.size会因为你pop和push而时刻改变
int size = q.size();
for(int i = 0; i < size; i++)
{
TreeNode* top = q.front();
q.pop();
tmp.push_back(top->val);
if(top->left) q.push(top->left);
if(top->right) q.push(top->right);
}
ans.push_back(tmp);
}
return ans;
}
};
时间复杂度:O(N),空间复杂度:O(N)
第一想法:反转102的数组即可
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
queue<TreeNode*> q;
if(root != nullptr) q.push(root);
vector<vector<int>> ans;
while(!q.empty())
{
vector<int> tmp;
//使用固定size,因为q.size会因为你pop和push而时刻改变
int size = q.size();
for(int i = 0; i < size; i++)
{
TreeNode* top = q.front();
q.pop();
tmp.push_back(top->val);
if(top->left) q.push(top->left);
if(top->right) q.push(top->right);
}
ans.push_back(tmp);
}
reverse(ans.begin(), ans.end());
return ans;
}
};
时间复杂度:O(N),空间复杂度:O(N)
第一想法:从右边能看到的元素就是每一层的最后一个元素,把它插入进数组即可
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> q;
if(root != nullptr) q.push(root);
vector<int> ans;
while(!q.empty())
{
int size = q.size();
for(int i = 0; i < size; i++)
{
TreeNode* top = q.front();
q.pop();
//插入每层最后一个元素
if(i == (size-1)) ans.push_back(top->val);
if(top->left) q.push(top->left);
if(top->right) q.push(top->right);
}
}
return ans;
}
};
时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> q;
vector<double> ans;
q.push(root);
while(!q.empty())
{
int size = q.size();
double sum = 0;
for(int i = 0; i < size; i++)
{
TreeNode* top = q.front();
q.pop();
sum += top->val;
if(top->left) q.push(top->left);
if(top->right) q.push(top->right);
}
ans.push_back(sum / size);
}
return ans;
}
};
时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
queue<Node*> q;
if(root != nullptr) q.push(root);
vector<vector<int>> ans;
while(!q.empty())
{
int size = q.size();
vector<int> tmp;
for(int i = 0; i < size; i++)
{
Node* top = q.front();
q.pop();
tmp.push_back(top->val);
//遍历children插入子节点即可
for(int j = 0; j < top->children.size(); j++)
q.push(top->children[j]);
}
ans.push_back(tmp);
}
return ans;
}
};
时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
queue<TreeNode*> q;
if(root != nullptr) q.push(root);
vector<int> ans;
while(!q.empty())
{
int size = q.size();
//取整数最小值
int maxNum = INT_MIN;
for(int i = 0; i < size; i++)
{
TreeNode* top = q.front();
q.pop();
maxNum = max(maxNum, top->val);
if(top->left) q.push(top->left);
if(top->right) q.push(top->right);
}
ans.push_back(maxNum);
}
return ans;
}
};
时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> q;
if(root != NULL) q.push(root);
while(!q.empty())
{
int size = q.size();
for(int i = 0; i < size; i++)
{
Node* top = q.front();
q.pop();
//将每层的左子树的next设置为右子树
if(i < (size-1)) top->next = q.front();
if(top->left) q.push(top->left);
if(top->right) q.push(top->right);
}
}
return root;
}
};
时间复杂度:O(N),空间复杂度:O(N)
和116的解题思路是一样的。
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> q;
if(root != NULL) q.push(root);
while(!q.empty())
{
int size = q.size();
for(int i = 0; i < size; i++)
{
Node* top = q.front();
q.pop();
if(i < (size-1)) top->next = q.front();
if(top->left) q.push(top->left);
if(top->right) q.push(top->right);
}
}
return root;
}
};
在day16中,我们已经做过了