【数据结构/二叉搜索树/修改与改造】题解+详细备注(共7题)

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

class Solution {
public:
	// 递归操作
    TreeNode* recursive(TreeNode* root, int val){
        if(!root){
            TreeNode* node = new TreeNode(val);
            return node;
        }  

        if(root->val > val) root->left = recursive(root->left,val);
        if(root->val < val) root->right = recursive(root->right,val);

        return root; 
    }

    TreeNode* insertIntoBST(TreeNode* root, int val) {
         //return recursive(root,val);
	
		 // 迭代方式实现
         if(!root){
             TreeNode* node = new TreeNode(val);
             return node;
         }

        TreeNode* cur = root;
        TreeNode* pre = root;
        while(cur != nullptr){
            pre = cur;
            if(cur->val > val) cur = cur->left;
            else cur = cur->right;
        }

        TreeNode* node = new TreeNode(val);
        if(pre->val > val) pre->left = node;
        else pre->right = node;

        return root;
    }
};

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

class Solution {
public:
	// 删除节点分5种情况
    TreeNode* deleteNode(TreeNode* root, int key) {
    	// 第一:没有找到节点,直接返回null,这也是递归终止条件
        if(!root) return {};
        if(root->val == key){
        	// 第二:左右子树都是空(叶子节点),直接删除节点,返回空
            if(!root->left && !root->right) return {};
            // 第三:右孩子为空,左孩子不为空,删除节点,左孩子补位
            else if(!root->right) return root->left;
            // 第四:左孩子为空,有孩子不为空,删除节点,右孩子补位
            else if(!root->left) return root->right;
            // 第五:左右孩子都不为空,则将删除节点的左子树放到删除节点的右子树最左面节点的左孩子位置
            else{
                TreeNode* left = root->left;
                TreeNode* right = root->right;
				// 找到右子树最左面的节点
                while(right->left){
                    right = right->left;
                }
				// 然后将删除节点的左子树放到删除节点的右子树最左面节点的左孩子位置
                right->left = left;
                // 返回右子树
                return root->right;
            }
        }
        // 递归左节点
        if(root->val > key) root->left = deleteNode(root->left,key);
        // 递归右节点
        if(root->val < key) root->right = deleteNode(root->right,key);

        return root;
    }
};

669.修剪二叉搜索树

class Solution {
public:
	// 递归
    // TreeNode* trimBST(TreeNode* root, int low, int high) {
    //     if(!root) return{};
    	   // 寻找符合区间[low,high]的节点
    //     if(root->val > high){
    //         return trimBST(root->left,low,high);
    //     }
			// 寻找符合区间[low,high]的节点
    //     if(root->val < low) return trimBST(root->right,low,high);
			// 接入符合条件的左孩子
    //     root->left = trimBST(root->left,low,high);
    		// 接入符合条件的右孩子
    //     root->right = trimBST(root->right,low,high);
    //     return root;
    // }
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if(!root) return{};
        // 迭代法实现
        // 将root移动到合理区间
        while(root && (root->val < low || root->val > high)){
            if(root->val > high) root = root->left;
            else root = root->right;
        }
        // 处理左孩子
        TreeNode* cur = root;
        while(cur){
            while(cur->left && (cur->left->val < low)){
                cur->left = cur->left->right;
            }

            cur = cur->left;
        }
        cur = root;
        // 处理右孩子
        while(cur){
            while(cur->right && (cur->right->val > high)){
                cur->right = cur->right->left;
            }
            cur = cur->right;
        }

        return root;
    }
};

108.将有序数组转化为二叉搜索树

class Solution {
public:
	// 构造二叉搜索树(核心是找中间节点)
    TreeNode* recursive(vector<int> &num,int left,int right){
    	// 递归终止条件
        if(left > right) return{};
		// 找中间节点
        int mid = (right + left)/2;
		
        TreeNode* root = new TreeNode(num[mid]);
		// 区分左右边界
        root->left = recursive(num,left,mid-1);
        root->right = recursive(num,mid+1,right);

        return root;
    }
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        TreeNode *root = recursive(nums,0,nums.size()-1);
        return root;
    }
};

1382.将二叉搜索树变平衡

class Solution {
private:
    vector<int> vec;
    // 有序树转成有序数组
    void traversal(TreeNode* cur) {
        if (cur == nullptr) {
            return;
        }
        traversal(cur->left);
        vec.push_back(cur->val);
        traversal(cur->right);
    }
    // 有序数组转平衡二叉树
    TreeNode* getTree(vector<int>& nums, int left, int right) {
        if (left > right) return nullptr;
        int mid = left + ((right - left) / 2);
        TreeNode* root = new TreeNode(nums[mid]);
        root->left = getTree(nums, left, mid - 1);
        root->right = getTree(nums, mid + 1, right);
        return root;
    }

public:
    TreeNode* balanceBST(TreeNode* root) {
        traversal(root);
        return getTree(vec, 0, vec.size() - 1);
    }
};

230.二叉搜索树中第K小的元素

// 中序遍历的应用
class Solution {
public:
    void inorder(TreeNode* root){
        if(!root) return;

        inorder(root->left);
        v.push_back(root->val);
        inorder(root->right);
    }
    int kthSmallest(TreeNode* root, int k) {
        if(!root) return -1;
        //中序遍历拿到排序好的数组,返回k-1下标元素
        inorder(root);
        return v[k-1];
    }
public:
    vector<int> v;
};

173.二叉搜索树迭代器

// 中序遍历的应用
class BSTIterator {
public:
    int curIndex{};
    vector<int> v;
public:
    void inorder(TreeNode* root){
        if(!root) return;

        inorder(root->left);
        v.emplace_back(root->val);
        inorder(root->right);
    }
    BSTIterator(TreeNode* root) {
        inorder(root);
    }
    
    int next() {
        return v[curIndex++];
    }
    
    bool hasNext() {
        if(curIndex < v.size()) return true;
        else return false;
    }

    
};
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一二三o-0-O

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值