您现在的位置是:首页 >学无止境 >代码随想录算法训练营第十五天 | 层序遍历 10,226.翻转二叉树,101.对称二叉树 2网站首页学无止境
代码随想录算法训练营第十五天 | 层序遍历 10,226.翻转二叉树,101.对称二叉树 2
简介代码随想录算法训练营第十五天 | 层序遍历 10,226.翻转二叉树,101.对称二叉树 2
代码随想录算法训练营第十五天 | 层序遍历 10,226.翻转二叉树,101.对称二叉树 2
1.1 层序遍历 10
1.1.1 102.二叉树的层序遍历
思路:
- 通过队列实现
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> q;
vector<vector<int>> res;
if(!root) return res;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> cur;
for(int i = 1; i <= size; ++i){
TreeNode* node = q.front();
q.pop();
cur.push_back(node->val);
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
res.push_back(cur);
}
return res;
}
};
1.1.2 二叉树的层次遍历 II
思路:
- 结果翻转一下
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
queue<TreeNode*> q;
vector<vector<int>> res;
if(!root) return res;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> cur;
for(int i = 1; i <= size; ++i){
TreeNode* node = q.front();
q.pop();
cur.push_back(node->val);
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
res.push_back(cur);
}
reverse(res.begin(), res.end());
return res;
}
};
1.1.3 二叉树的右视图
思路:
- 只有是当前层的最后一个才加入
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
queue<TreeNode*> q;
vector<int> res;
if(!root) return res;
q.push(root);
while(!q.empty()){
int size = q.size();
for(int i = 1; i <= size; ++i){
TreeNode* node = q.front();
q.pop();
if(i == size) res.push_back(node->val);
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
return res;
}
};
1.1.4 二叉树的层平均值
思路:
- sum记录每层和即可
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> q;
vector<double> res;
if(!root) return res;
q.push(root);
while(!q.empty()){
int size = q.size();
double sum = 0;
for(int i = 1; i <= size; ++i){
TreeNode* node = q.front();
q.pop();
sum += node->val;
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
res.push_back(sum / size);
}
return res;
}
};
1.1.5 N叉树的层序遍历
思路:
- 添加的节点数从2个变为复数个
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
queue<Node*> q;
vector<vector<int>> res;
if(!root) return res;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> cur;
for(int i = 1; i <= size; ++i){
Node* node = q.front();
q.pop();
cur.push_back(node->val);
for(int j = 0; j < node->children.size(); ++j){
if(node->children[j]) q.push(node->children[j]);
}
}
res.push_back(cur);
}
return res;
}
};
1.1.6 515.在每个树行中找最大值
思路:
- 每层比较即可
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
queue<TreeNode*> q;
vector<int> res;
if(!root) return res;
q.push(root);
while(!q.empty()){
int size = q.size();
int max = INT_MIN;
for(int i = 1; i <= size; ++i){
TreeNode* node = q.front();
q.pop();
max = max < node->val ? node->val : max;
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
res.push_back(max);
}
return res;
}
};
1.1.7 116.填充每个节点的下一个右侧节点指针
思路:
- 理清关系即可
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> q;
if(!root) return root;
q.push(root);
while(!q.empty()){
int size = q.size();
for(int i = 1; i <= size; ++i){
Node* node = q.front();
q.pop();
if(i != size) node->next = q.front();
else node->next = nullptr;
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
return root;
}
};
1.1.8 117.填充每个节点的下一个右侧节点指针II
思路:
- 同上,没有区别
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> q;
if(!root) return root;
q.push(root);
while(!q.empty()){
int size = q.size();
for(int i = 1; i <= size; ++i){
Node* node = q.front();
q.pop();
if(i != size) node->next = q.front();
else node->next = nullptr;
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
return root;
}
};
1.1.9 104.二叉树的最大深度
思路:
- 使用迭代法的话,可以使用层序遍历的方式
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == NULL) return 0;
int depth = 0;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()) {
int size = que.size();
depth++; // 记录深度
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);
}
}
return depth;
}
};
思路:
- 也可以使用递归的方式
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == nullptr) return 0;
return max(maxDepth(root->right), maxDepth(root->left)) +1;
}
};
1.1.10 111.二叉树的最小深度
思路:
- 当左右孩子都为空时,就可以结束了
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr) return 0;
int depth = 0;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()) {
int size = que.size();
depth++;
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);
if (!node->left && !node->right) return depth;
}
}
return depth;
}
};
1.2 226.翻转二叉树
思路:
- 递归(前序遍历)
- 迭代-深度优先(前序遍历+栈)
- 迭代-广度优先(层序遍历+队列)
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) {
if (root == NULL) return root;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()) {
TreeNode* node = st.top(); // 中
st.pop();
swap(node->left, node->right);
if(node->right) st.push(node->right); // 右
if(node->left) st.push(node->left); // 左
}
return root;
}
};
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
swap(node->left, node->right); // 节点处理
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return root;
}
};
1.3 101. 对称二叉树
思路:
- 比较的不是左右节点,而是左右子树
递归方式(1)
class Solution {
public:
bool compare(TreeNode* left, TreeNode* right) {
// 首先排除空节点的情况
if (left == NULL && right != NULL) return false;
else if (left != NULL && right == NULL) return false;
else if (left == NULL && right == NULL) return true;
// 排除了空节点,再排除数值不相同的情况
else if (left->val != right->val) return false;
// 此时就是:左右节点都不为空,且数值相同的情况
// 此时才做递归,做下一层的判断
bool outside = compare(left->left, right->right); // 左子树:左、 右子树:右
bool inside = compare(left->right, right->left); // 左子树:右、 右子树:左
bool isSame = outside && inside; // 左子树:中、 右子树:中 (逻辑处理)
return isSame;
}
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
return compare(root->left, root->right);
}
};
递归方式(2)
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return dfs(root, root);
}
private:
bool dfs(TreeNode* tree1, TreeNode* tree2){
if(tree1 == nullptr && tree2 == nullptr) return true;
if(tree1 == nullptr || tree2 == nullptr) return false;
if(tree1->val == tree2->val){
return dfs(tree1->left, tree2->right) && dfs(tree1->right, tree2->left);
}
else{
return false;
}
}
};
迭代+队列(栈也可以)
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
queue<TreeNode*> que;
que.push(root->left); // 将左子树头结点加入队列
que.push(root->right); // 将右子树头结点加入队列
while (!que.empty()) { // 接下来就要判断这两个树是否相互翻转
TreeNode* leftNode = que.front(); que.pop();
TreeNode* rightNode = que.front(); que.pop();
if (!leftNode && !rightNode) { // 左节点为空、右节点为空,此时说明是对称的
continue;
}
// 左右一个节点不为空,或者都不为空但数值不相同,返回false
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
return false;
}
que.push(leftNode->left); // 加入左节点左孩子
que.push(rightNode->right); // 加入右节点右孩子
que.push(leftNode->right); // 加入左节点右孩子
que.push(rightNode->left); // 加入右节点左孩子
}
return true;
}
};
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。