代码随想录一刷打卡——二叉树(下篇)

文章目录

  • 前言
  • 一、98. 验证二叉搜索树
  • 二、530. 二叉搜索树的最小绝对差
  • 三、501. 二叉搜索树中的众数
  • 四、236. 二叉树的最近公共祖先
  • 五、235. 二叉搜索树的最近公共祖先
  • 六、701. 二叉搜索树中的插入操作
  • 七、450. 删除二叉搜索树中的节点
  • 八、669. 修剪二叉搜索树
  • 九、108. 将有序数组转换为二叉搜索树
  • 十、538. 把二叉搜索树转换为累加树
  • 总结


前言

一个本硕双非的小菜鸡,备战24年秋招,计划刷完卡子哥的刷题计划,加油!
推荐一手卡子哥的刷题网站,感谢卡子哥。代码随想录

一、98. 验证二叉搜索树

98. 验证二叉搜索树
Note:可以先使用中序遍历存储到数组中,再判断是否有序。


/**
* 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 {
private:
  vector<int> vec;
  void traversal(TreeNode* root) {
      if (root == NULL) return;
      traversal(root->left);
      vec.push_back(root->val);
      traversal(root->right);
  }
public:
  bool isValidBST(TreeNode* root) {
      vec.clear();
      traversal(root);
      for (int i = 1; i < vec.size(); i++) {
          if (vec[i] <= vec[i - 1])
              return false;
      }
      return true;
  }
};

二、530. 二叉搜索树的最小绝对差

530. 二叉搜索树的最小绝对差

Note:跟98类似,一样的变成数组来判断,很舒服


/**
* 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 {
private:
vector<int> vec;
void travelsal(TreeNode* root) {
  if (root == NULL) return;
  travelsal(root->left);
  vec.push_back(root->val);
  travelsal(root->right);
}
public:
  int getMinimumDifference(TreeNode* root) {
      vec.clear();
      travelsal(root);
      if (vec.size() < 2) return 0;
      int result = INT_MAX;
      for (int i = 1; i < vec.size(); i++) {
          result = min (result, vec[i] - vec[i - 1]);
      }
      return result;
  }
};

注:此题与783. 二叉搜索树节点最小距离一模一样。

三、501. 二叉搜索树中的众数

501. 二叉搜索树中的众数

Note:转有序数组


/**
* 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 {
private:
  int maxCount = 0;
  int count = 0;
  TreeNode* pre = NULL;
  vector<int> result;
  void searchBST(TreeNode* cur) {
      if (cur == NULL) return;

      searchBST(cur->left);

      if (pre == NULL) {
          count = 1;
      } else if (pre->val == cur->val) {
          count++;
      } else {
          count = 1;
      }
      pre = cur;

      if (count == maxCount) {
          result.push_back(cur->val);
      }

      if (count > maxCount) {
          maxCount = count;
          result.clear();
          result.push_back(cur->val);
      }

      searchBST(cur->right);
      return;
  }
public:
  vector<int> findMode(TreeNode* root) {
      count = 0;
      maxCount = 0;
      pre = NULL;
      result.clear();

      searchBST(root);
      return result;
  }
};

四、236. 二叉树的最近公共祖先

236. 二叉树的最近公共祖先

Note:搜索边与搜索整棵树的写法不同,边是直接返回,树是用变量left、right接住返回值

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == q || root == p || root == NULL) return root;

        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);

        if (left != NULL && right != NULL) return root;
        if (left == NULL && right != NULL) return right;
        else if (left != NULL && right == NULL) return left;
        else return NULL;
    }
};

五、235. 二叉搜索树的最近公共祖先

235. 二叉搜索树的最近公共祖先

Note:跟236类似

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
private:
    TreeNode* traversal(TreeNode* cur, TreeNode* p, TreeNode* q) {
        if (cur == NULL) return cur;

        if (cur->val > p->val && cur->val > q->val) {
            TreeNode* left = traversal(cur->left, p, q);
            if (left != NULL) {
                return left;
            }
        }

        if (cur->val < p->val && cur->val < q->val) {
            TreeNode* right = traversal(cur->right, p, q);
            if (right != NULL) {
                return right;
            }
        }
        return cur;
    }
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        return traversal(root, p, q);
    }
};

六、701. 二叉搜索树中的插入操作

701. 二叉搜索树中的插入操作

Note:这题算不上中等,根据搜索树性质插就行了

/**
 * 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* insertIntoBST(TreeNode* root, int val) {
        if (root == NULL) {
            TreeNode* node = new TreeNode(val);
            return node;
        }

        if (root->val > val) root->left = insertIntoBST(root->left, val);
        if (root->val < val) root->right = insertIntoBST(root->right, val);
        return root;
    }
};

七、450. 删除二叉搜索树中的节点

  1. 删除二叉搜索树中的节点](https://leetcode.cn/problems/delete-node-in-a-bst/description/)

Note:分为五种情况都需要讨论

/**
 * 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* deleteNode(TreeNode* root, int key) {
        if (root == NULL) return root;

        if (root->val == key) {
            if (root->left == NULL && root->right == NULL) {
                delete root;
                return NULL;
            }
            else if (root->left == NULL && root->right != NULL) {
                auto retNode = root->right;
                delete root;
                return retNode;
            }
            else if (root->left != NULL && root->right == NULL) {
                auto retNode = root->left;
                delete root;
                return retNode;
            }
            else {
                TreeNode* cur = root->right;
                while(cur->left != NULL) {
                    cur = cur->left;
                }
                cur->left = root->left;
                TreeNode* tmp = root;
                root = root->right;
                delete tmp;
                return root;
            }
        }
        if (root->val > key) root->left = deleteNode(root->left, key);
        if (root->val < key) root->right = deleteNode(root->right, key);

        return root;
    }
};

八、669. 修剪二叉搜索树

669. 修剪二叉搜索树

Note:递归,但是要考虑删除节点的左右子树

/**
 * 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* trimBST(TreeNode* root, int low, int high) {
        if (root == NULL) return NULL;
        if (root->val < low) {
            TreeNode* right = trimBST(root->right, low, high);
            return right;
        }
        if (root->val > high) {
            TreeNode* left = trimBST(root->left, low, high);
            return left;
        }        
        root->left = trimBST(root->left, low, high);
        root->right = trimBST(root->right, low, high);
        return root;
    }
};

九、108. 将有序数组转换为二叉搜索树

108. 将有序数组转换为二叉搜索树

Note:递归遍历,从中间一个个往上加

/**
 * 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 {
private:
    TreeNode* traversal(vector<int>& nums, int left, int right) {
        if (left > right) return NULL;

        int mid = left + ((right - left) / 2);

        TreeNode* root = new TreeNode(nums[mid]);
        root->left = traversal(nums, left, mid - 1);
        root->right = traversal(nums, mid + 1, right);
        return root;
    }
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        TreeNode* root = traversal(nums, 0, nums.size() - 1);
        return root;
    }
};

十、538. 把二叉搜索树转换为累加树

538. 把二叉搜索树转换为累加树

Note:一样先转成数组(注意要右中左遍历),然后再依次相加。



/**
* 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 {
private:
  int pre = 0;
  void travesal(TreeNode* cur) {
      if (cur == NULL) return;
      travesal(cur->right);
      cur->val += pre;
      pre = cur->val;
      travesal(cur->left);
  }
public:
  TreeNode* convertBST(TreeNode* root) {
      pre = 0;
      travesal(root);
      return root;
  }
};

总结

因为二叉树题比较多,就分上中下三篇记录了,系统的搞懂了前中后的两种遍历方式,二叉树是很多算法的基础,非常重要!代码随想录一刷打卡——二叉树(下篇)
啊,二叉树终于结束了,题很多很好,不愧是数据结构中最多花样的,继续努力吧。

你可能感兴趣的:(代码随想录刷刷刷,学习,程序人生,leetcode,c++)