Day19 | LeetCode 669. 修剪二叉搜索树, 108. 将有序数组转换为二叉搜索树, 538. 把二叉搜索树转换为累加树

LeetCode 669. 修剪二叉搜索树

递归
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if (root == nullptr) {
            return nullptr;
        }

        root->left = trimBST(root->left, low, high);
        root->right = trimBST(root->right, low, high);
        if (root->val < low) {
            root = root->right;
        } else if (root->val > high) {
            root = root->left;
        }
        return root;
    }
};
迭代
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if (!root) {
            return nullptr;
        }
        while (root && (root->val < low || root->val > high)) {
            if (root->val < low) {
                root = root->right;
            } else {
                root = root->left;
            }
        }
        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;
    }
};

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

class Solution {
public:
    
    TreeNode* traversal(vector<int>& nums, int start, int end) {
        if (start == end) {
            return nullptr;
        }
        //中点位置
        int mid = start + (end - start) / 2;
        TreeNode* root = new TreeNode(nums[mid]);
        if (end - start == 1) {
            return root;
        }

        root->left = traversal(nums, start, mid);
        root->right = traversal(nums, mid + 1, end);

        return root;
    }

    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return traversal(nums, 0, nums.size());
    }
};

LeetCode 538. 把二叉搜索树转换为累加树

自己实现版本(递归)
class Solution {
public:
    int pre = 0;
    TreeNode* convertBST(TreeNode* root) {
        if (!root) {
            return nullptr;
        }
        TreeNode* right = convertBST(root->right);
        if (right && !pre){
            pre = right->val;
        }
        root->val += pre;
        pre = root->val;
        convertBST(root->left);
        return root;     
    }
};
精简版本
class Solution {
public:
    int pre = 0;
    void traversal(TreeNode* root) {
        if (!root) {
            return;
        }
        traversal(root->right);
        root->val += pre;
        pre = root->val;
        traversal(root->left);
    }

    TreeNode* convertBST(TreeNode* root) {
        traversal(root);
        return root;
    }
};
迭代
class Solution {
public:
    TreeNode* convertBST(TreeNode* root) {
        if(root == nullptr) {
            return nullptr;
        }
        stack<TreeNode*> sta;
        int pre = 0;
        TreeNode* cur = root;
        while (!sta.empty() || root) {
            if (root) {
                sta.push(root);
                root = root->right;
            }else {
                root = sta.top();
                root->val += pre;
                pre = root->val;
                sta.pop();
                root = root->left;
            }
        }
        return cur;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值