代码随想录算法训练营第二十二天 | 力扣 235 二叉搜索树的最近公共祖先 力扣 701.二叉搜索树中的插入操作 力扣 450.删除二叉搜索树中的节点

day22 记录代码随想录

第一题 力扣 235 二叉搜索树的最近公共祖先

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。"

题目链接:力扣题目链接

代码如下:

cpp 复制代码
/**
 * 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 == NULL)
            return root;

        if(root->val > p->val && root->val > q->val) {
            TreeNode* left = lowestCommonAncestor(root->left, p, q);
            if(left != NULL)
                return left;
        }
        else if(root->val < p->val && root->val < q->val) {
            TreeNode* right = lowestCommonAncestor(root->right, p, q);
            if(right != NULL)
                return right;
        }
        return root;
    }
};

第二题 力扣 701.二叉搜索树中的插入操作

给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。

注意 ,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果

题目链接:力扣题目链接

cpp 复制代码
/**
 * 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.删除二叉搜索树中的节点

给定一个二叉搜索树的根节点 root 和一个值 key ,删除二叉搜索树中的 key对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。

一般来说,删除节点可分为两个步骤:

  1. 首先找到需要删除的节点;
  2. 如果找到了,删除它。

题目链接:力扣题目链接

代码如下:

cpp 复制代码
/**
 * 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) {
                TreeNode* temp = root->right;
                delete root;
                return temp;
            }
            else if(root->left != NULL && root->right == NULL) {
                TreeNode* temp = root->left;
                delete root;
                return temp;
            }
            else {
                TreeNode* node = root->right;
                while(node->left != NULL) {
                    node = node->left;
                }
                node->left = root->left;
                TreeNode* temp = root->right;
                delete root;
                return temp;
            }
        }
        if(root->val > key)
            root->left = deleteNode(root->left, key);
        if(root->val < key)
            root->right = deleteNode(root->right,key);
        return root;

    }
};
相关推荐
米粒132 分钟前
力扣算法刷题 Day 31 (贪心总结)
算法·leetcode·职场和发展
少许极端36 分钟前
算法奇妙屋(四十)-贪心算法学习之路7
java·学习·算法·贪心算法
AlenTech1 小时前
647. 回文子串 - 力扣(LeetCode)
算法·leetcode·职场和发展
py有趣1 小时前
力扣热门100题之合并两个有序链表
算法·leetcode·链表
8Qi81 小时前
LeetCode热题100--45.跳跃游戏 II
java·算法·leetcode·贪心算法·编程
foundbug9992 小时前
基于STM32的步进电机加减速程序设计(梯形加减速算法)
stm32·单片机·算法
北顾笙9802 小时前
day12-数据结构力扣
数据结构·算法·leetcode
凌波粒2 小时前
LeetCode--454.四数相加 II(哈希表)
算法·leetcode·散列表
漫随流水2 小时前
c++编程:D进制的A+B(1022-PAT乙级)
数据结构·c++·算法
tankeven2 小时前
HJ159 没挡住洪水
c++·算法