代码随想录算法训练营第十八天
二叉树的层序遍历
102.二叉树的层序遍历
题目链接:102.二叉树的层序遍历
创建一个队列存储节点,循环内弹出节点时,将它的子节点同时加入队列,在每层循环之前记录队列内一层节点的数量,保证弹出的时候只弹出本层节点。外层循环控制层数,内层循环控制每个节点的弹出。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> results;
queue<TreeNode*> que;
if(root!=nullptr)que.push(root);
while(!que.empty()){
vector<int> result;
int size = que.size();
for(int i = size;i>0;i--){
auto cur = que.front();
result.push_back(cur->val);
que.pop();
if(cur->left)que.push(cur->left);
if(cur->right)que.push(cur->right);
}
results.push_back(result);
}
return results;
}
};
107.二叉树的层次遍历II
题目链接:107.二叉树的层次遍历II
解法和第一题相同,最后需要翻转结果数组,让底层叶子结点在数组开头。
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> results;
queue<TreeNode*> que;
if (root!=nullptr)que.push(root);
while(!que.empty()){
vector<int> result;
int size = que.size();
for(int i =0;i<size;i++){
auto cur = que.front();
result.push_back(cur->val);
que.pop();
if(cur->left)que.push(cur->left);
if(cur->right)que.push(cur->right);
}
results.push_back(result);
}
reverse(results.begin(), results.end());
return results;
}
};
199.二叉树的右视图
题目链接:199.二叉树的右视图
解法和第一题相同,将每层最后一个节点放入结果数组内。
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
queue<TreeNode*> que;
if(root!=nullptr)que.push(root);
while(!que.empty()){
int size = que.size();
for(int i = 0;i<size;i++){
auto cur = que.front();
que.pop();
if(cur->left)que.push(cur->left);
if(cur->right)que.push(cur->right);
if(i==size-1)result.push_back(cur->val);
}
}
return result;
}
};
637.二叉树的层平均值
题目链接:637.二叉树的层平均值
层序遍历,将每层的节点值加和最后除以节点个数。
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> result;
queue<TreeNode*> que;
if (root != nullptr)
que.push(root);
while (!que.empty()) {
int size = que.size();
double sum = 0;
for (int i = 0; i < size; i++) {
auto cur = que.front();
que.pop();
if (cur->left)
que.push(cur->left);
if (cur->right)
que.push(cur->right);
sum+=cur->val;
}
result.push_back(sum/size);
}
return result;
}
};
429.N叉树的层序遍历
题目链接:429.N叉树的层序遍历
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> results;
queue<Node*> que;
if (root != nullptr)
que.push(root);
while (!que.empty()) {
int size = que.size();
vector<int> result;
for (int i = 0; i < size; i++) {
auto cur = que.front();
result.push_back(cur->val);
que.pop();
for(auto &j:cur->children){
que.push(j);
}
}
results.push_back(result);
}
return results;
}
};
515.在每个树行中找最大值
题目链接:515.在每个树行中找最大值
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> result;
queue<TreeNode*> que;
if (root != nullptr)
que.push(root);
while (!que.empty()) {
int size = que.size();
int max_val = que.front()->val;
for (int i = 0; i < size; i++) {
auto cur = que.front();
que.pop();
if (cur->left)
que.push(cur->left);
if (cur->right)
que.push(cur->right);
max_val = max(max_val,cur->val);
}
result.push_back(max_val);
}
return result;
}
};
116.填充每个节点的下一个右侧节点指针
题目链接:116.填充每个节点的下一个右侧节点指针
层序遍历,将当前处理的节点的next指向栈顶,计数到本层最后一个节点将它的next置空。
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root != nullptr)
que.push(root);
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
auto cur = que.front();
que.pop();
if(i ==size-1) cur->next =nullptr;
else cur->next = que.front();
if(cur->left)que.push(cur->left);
if(cur->right)que.push(cur->right);
}
}
return root;
}
};
117.填充每个节点的下一个右侧节点指针II
题目链接:117.填充每个节点的下一个右侧节点指针II
和上题一模一样。
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> que;
if (root != nullptr)
que.push(root);
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
auto cur = que.front();
que.pop();
if(i ==size-1) cur->next =nullptr;
else cur->next = que.front();
if(cur->left)que.push(cur->left);
if(cur->right)que.push(cur->right);
}
}
return root;
}
};
104.二叉树的最大深度
题目链接:104.二叉树的最大深度
层序遍历后返回层数即可。
class Solution {
public:
int maxDepth(TreeNode* root) {
vector<vector<int>> results;
queue<TreeNode*> que;
if(root!=nullptr)que.push(root);
while(!que.empty()){
vector<int> result;
int size = que.size();
for(int i = size;i>0;i--){
auto cur = que.front();
result.push_back(cur->val);
que.pop();
if(cur->left)que.push(cur->left);
if(cur->right)que.push(cur->right);
}
results.push_back(result);
}
return results.size();
}
};
111.二叉树的最小深度
题目链接:111.二叉树的最小深度
层序遍历,当左右节点都为空时返回深度值为最小深度。
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
int depth = 0;
if (root != nullptr)
que.push(root);
while (!que.empty()) {
vector<int> result;
int size = que.size();
depth++;
for (int i = size; i > 0; i--) {
auto cur = que.front();
result.push_back(cur->val);
que.pop();
if (cur->left)
que.push(cur->left);
if (cur->right)
que.push(cur->right);
if (!cur->left&&!cur->right){
return depth;
}
}
}
return depth;
}
};
226.翻转二叉树
题目链接:226.翻转二叉树
前序遍历,先交换中间的,然后交换左节点,最后交换右节点
递归遍历:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root ==nullptr)return root;
swap(root->left,root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
迭代遍历:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
stack<TreeNode*> st;
if(root!=nullptr)st.push(root);
while (!st.empty()){
auto cur =st.top();
swap(cur->left,cur->right);
st.pop();
if(cur->left){st.push(cur->left);}
if(cur->right){st.push(cur->right);}
}
return root;
}
};
101.对称二叉树
题目链接:101.对称二叉树
非对称的条件:
1. 左子空,右子不空
2. 左子不空,右子空
3. 左子值不等于右子值
对称条件:
1. 左子空,右子空
2. 左子值=右子值(继续循环)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
* right(right) {}
* };
*/
class Solution {
public:
bool compair(TreeNode* left, TreeNode* right) {
if (left == nullptr && right != nullptr)
return false;
else if (left != nullptr && right == nullptr)
return false;
else if (left == nullptr && right == nullptr)
return true;
else if (left->val != right->val)
return false;
bool outside = compair(left->left, right->right);
bool inside = compair(left->right, right->left);
return outside && inside;
}
bool isSymmetric(TreeNode* root) {
bool result = compair(root->left, root->right);
return result;
}
};