• 代码随想录刷题 Day 22


    235. 二叉搜索树的最近公共祖先 
    具体思路就是当小于pq的时候就往右取遍历,当大于的时候就往左遍历;
    1. lass Solution {
    2. public:
    3. TreeNode* traversal(TreeNode* current, TreeNode* p, TreeNode* q) {
    4. if (current->val > p->val && current->val > q->val) { //如果根节点大于pq,就往左子树找
    5. TreeNode* left = traversal(current->left, p, q); //如果左子树找到了的话就定义一个treenode类型的变量把这个数值接住
    6. //if (left != NULL) { //由于题目写了肯定找得到,所以这个判断语句不写也能过
    7. return left;
    8. // }//如果没找到left就是空的
    9. }
    10. if (current->val < p->val && current->val < q->val) { //如果根节点大于pq,就往左子树找
    11. TreeNode* right = traversal(current->right, p, q); //如果左子树找到了的话就定义一个treenode类型的变量把这个数值接住
    12. //if (right != NULL) {
    13. return right;
    14. // }//如果没找到那right就是空的
    15. }
    16. return current;//这就是第三中情况,不走前面两种if的时候就是找到了直接输出;
    17. }
    18. TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    19. TreeNode *result = new TreeNode(0);
    20. result = traversal(root, p, q);
    21. return result;
    22. }
    23. };

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

    简单题自己可以写出来,这个题递归函数需要返回值不然还需要记录下前一个节点的位置,然后再让前一个节点指向后一个节点

    1. class Solution {
    2. public:
    3. TreeNode* insertIntoBST(TreeNode* root, int val) {
    4. //TreeNode* extra_node = new TreeNode(val);
    5. if (root == NULL) {
    6. TreeNode* extra_node = new TreeNode(val);
    7. root = extra_node;
    8. return root;
    9. }
    10. if (root->val > val) {
    11. root->left = insertIntoBST(root->left, val);
    12. }
    13. if (root->val < val) {
    14. root->right = insertIntoBST(root->right, val);
    15. }
    16. return root;
    17. }
    18. };

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

    总体结构还是比较简单的,就是五种分类方法有点麻烦

    1. class Solution {
    2. public:
    3. TreeNode* deleteNode(TreeNode* root, int key) {
    4. if (root == nullptr) return root; // 第一种情况:没找到删除的节点,遍历到空节点直接返回了
    5. if (root->val == key) {
    6. // 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
    7. if (root->left == nullptr && root->right == nullptr) {
    8. ///! 内存释放
    9. delete root;
    10. return nullptr;
    11. }
    12. // 第三种情况:其左孩子为空,右孩子不为空,删除节点,右孩子补位 ,返回右孩子为根节点
    13. else if (root->left == nullptr) {
    14. TreeNode* retNode = root->right;
    15. ///! 内存释放
    16. delete root;
    17. return retNode;
    18. }
    19. // 第四种情况:其右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
    20. else if (root->right == nullptr) {
    21. TreeNode* retNode = root->left;
    22. ///! 内存释放
    23. delete root;
    24. return retNode;
    25. }
    26. // 第五种情况:左右孩子节点都不为空,则将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置
    27. // 并返回删除节点右孩子为新的根节点。
    28. else {
    29. TreeNode* cur = root->right; // 找右子树最左面的节点
    30. while(cur->left != nullptr) {
    31. cur = cur->left;
    32. }
    33. cur->left = root->left; // 把要删除的节点(root)左子树放在cur的左孩子的位置
    34. TreeNode* tmp = root; // 把root节点保存一下,下面来删除
    35. root = root->right; // 返回旧root的右孩子作为新root
    36. delete tmp; // 释放节点内存(这里不写也可以,但C++最好手动释放一下吧)
    37. return root;
    38. }
    39. }
    40. if (root->val > key) root->left = deleteNode(root->left, key);
    41. if (root->val < key) root->right = deleteNode(root->right, key);
    42. return root;
    43. }
    44. };

  • 相关阅读:
    python基于django药房药品销售进销存管理系统
    【网络协议】Http-上
    算法竞赛进阶指南 基本算法 0x07 贪心
    pod生命周期回调prestop、poststart源码分析
    给饿了么Radio 单选框添加点击事件
    pytorch -- 构建自己的Dateset,DataLoader如何使用
    C/C++语言100题练习计划 99——找第一个只出现一次的字符
    【SpringBoot篇】分页查询 | 扩展SpringMvc的消息转换器
    windows 安装 MySQL 绿色版
    蓝桥等考Python组别六级003
  • 原文地址:https://blog.csdn.net/weixin_43908951/article/details/133379605