树07

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

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 == nullptr) return root;
        if (root -> val == key){
            if (root -> left == nullptr && root -> right == nullptr){
                delete root;
                return nullptr;
            }
            if (root -> left == nullptr && root -> right != nullptr){
                TreeNode * autoright = root -> right;
                delete root;
                return autoright;
            }
            if (root -> left != nullptr && root -> right == nullptr){
                TreeNode * autoleft = root -> left;
                delete root;
                return autoleft;
            }
            if (root -> left != nullptr && root -> right != nullptr){
                TreeNode* cur = root -> right;
                while(cur -> left)
                    cur = cur -> left;
                cur -> left = root -> left;
                TreeNode* autoright = root -> right;
                delete root;
                return autoright;
            }
        }
        if (key < root -> val) root -> left = deleteNode(root -> left, key);
        if (key > root -> val) root -> right = deleteNode(root -> right, key);
        return root;
    }
};

669. 修剪二叉搜索树

/**
 * 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 == nullptr) return nullptr;
        if (root -> val < low)
        {
            auto right = trimBST(root -> right, low, high);
           
            return right;
        }
        if (root -> val > high)
        {
           auto left = trimBST(root -> left, low, high);
        
           return left;
        }
        root -> left = trimBST(root -> left, low, high);
        root -> right = trimBST(root -> right, low, high);
        return root;
    }
};
posted @ 2025-08-02 16:36  skyler886  阅读(7)  评论(0)    收藏  举报