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

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

题目链接:235
思路:若p,q在节点的两侧,则该节点一定是最近公共祖先;在搜索树中,在pq两侧说明节点大小在 [ p , q ] [p,q] [p,q],因此大小在 [ p , q ] [p,q] [p,q]的节点一定为最近公共祖先。
代码:

/**
 * 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) return NULL;

        if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q);
        if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q);
        return root;
    }
};

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

题目链接:701
思路:只需在原二叉树不变的情况下,根据大小关系找到空节点并在该位置插入,即可满足要求。
代码:

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

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

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

题目链接:450
思路:若当前节点是要删除的节点,则处理完后返回,否则处理左右。
代码:

/**
 * 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) return NULL;
        if (root->val == key) {
            if (!root->left && !root->right) {
                delete root;
                return NULL;
            }
            if (root->left && !root->right) {
                TreeNode* new_root = root->left;
                delete root;
                return new_root;
            };
            if (!root->left && root->right) {
                TreeNode* new_root = root->right;
                delete root;
                return new_root;
            };
            if (root->left && root->right) {
                TreeNode* temp = root->right;
                while (temp->left) temp = temp->left;
                temp->left = root->left;
                TreeNode* new_root = root->right;
                delete root;
                return new_root;
            }
        }

        if (key < root->val) root->left = deleteNode(root->left, key);
        if (key > root->val) root->right = deleteNode(root->right, key);
        return root;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值