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

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

代码:递归法 

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

 note:老天,我把大小于搞反了

代码:迭代法

/**
 * 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) {
        stack<TreeNode*> st;
        if(root == NULL) return NULL;
        st.push(root);
        while(root){
            if(p->val < root->val && q->val < root->val){
                root = root->left;
            }else if(p->val > root->val && q->val > root->val){
                root = root->right;
            }else{
                return root;
            }
        } 
        return NULL;
    }
};

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

代码: 

/**
 * 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(val < root->val){
            root->left = insertIntoBST(root->left,val);
        }
        if(val > root->val){
            root->right = insertIntoBST(root->right,val);
        }
        return root;
    }
};

 note:就正常遍历二叉搜索树,最后遍历到哪里就把新结点插到哪里。

代码:迭代法 

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == NULL){
            TreeNode* node = new TreeNode(val);
            return node;
        }
        TreeNode* cur = root;
        TreeNode* parent = root;
        while(cur){
            parent = cur;
            if(val < cur->val){
                cur = cur->left;
            }else if(val > cur->val){
                cur = cur->right;
            }
        }
        TreeNode* node = new TreeNode(val);
        if(val < parent->val){
            parent->left = node;
        }else{
            parent->right = node;
        }
        return root;
    }
};

note:需要找到合适的parent结点进行插入,因此我们定义了parent指针来记录cur的上一次的值。

3.删除二叉搜索树中的结点

代码:递归法 

/**
 * 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) {
        // 情况1:找不到要删除的结点
        if(root == NULL) return NULL;
        // 中
        if(root->val == key){
            // 情况2:左右子树都为空
            if(root->left == NULL && root->right == NULL){
                return NULL;
            }
            // 情况3:右子树为空
            if(root->left != NULL && root->right == NULL){
                return root->left;
            }
            // 情况4:左子树为空
            if(root->left == NULL && root->right != NULL){
                return root->right;
            }
            // 情况5:左右子树都不为空
            TreeNode* cur = root->right;
            while(cur->left){
                cur = cur->left;
            }
            cur->left = root->left;
            TreeNode* temp = root;
            root = root->right;
            delete temp;
            return root;
        }
        if(key < root->val){
            root->left = deleteNode(root->left,key);
        }
        if(key > root->val){
            root->right = deleteNode(root->right,key);
        }
        return root;
    }
};

 note:当待删除结点的左右子树都不为空时,找到其右子树的最左结点,然后让此节点的左子树赋值为待删除结点的左子树,然后返回待删除结点的右子树即可。

普通二叉树的删除:

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值