代码随想录Day23|669.修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.吧二叉搜索树转换为累加树、总结

文章目录

  • 669.修剪二叉搜索树
  • 108.将有序数组转换为二叉搜索树
  • 538.把二叉搜索树转换为累加树
  • 总结

669.修剪二叉搜索树

文章讲解:代码随想录 (programmercarl.com)

视频讲解:669.修剪二叉搜索树

题目链接:669. 修剪二叉搜索树 - 力扣(LeetCode)

题目: 给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。

递归

class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if (root == nullptr) return root;
        if (root->val < low) {
            TreeNode* right = trimBST(root->right, low, high);
            return right;
        }
        if (root->val > high) {
            TreeNode* left = trimBST(root->left, low, high);
            return left;
        }
        root->left = trimBST(root->left, low, high);
        root->right = trimBST(root->right, low, high);
        return root;
    }
};

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

文章讲解:代码随想录 (programmercarl.com)

视频讲解:108.将有序数组转换为二叉搜索树

题目链接:108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode)

题目: 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。

递归

class Solution {
public:
    TreeNode* traversal (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 = traversal(nums, left, mid - 1);
        root->right = traversal(nums, mid + 1, right);
        return root;
    }
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return traversal(nums, 0, nums.size() - 1);
    }
};

迭代

class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        if (nums.size() == 0) return nullptr;
        TreeNode* root = new TreeNode(0);
        queue<TreeNode*> nodeQue;
        queue<int> leftQue;
        queue<int> rightQue;
        nodeQue.push(root);
        leftQue.push(0);
        rightQue.push(nums.size() - 1);
        while (!nodeQue.empty()) {
            TreeNode* curNode = nodeQue.front(); nodeQue.pop();
            int left = leftQue.front(); leftQue.pop();
            int right = rightQue.front(); rightQue.pop();
            int mid = left + (right - left) / 2;
            curNode->val = nums[mid];
            
            if (left <= mid - 1) {
                curNode->left = new TreeNode(0);
                nodeQue.push(curNode->left);
                leftQue.push(left);
                rightQue.push(mid - 1);
            }
            if (right >= mid + 1) {
                curNode->right = new TreeNode(0);
                nodeQue.push(curNode->right);
                leftQue.push(mid + 1);
                rightQue.push(right);
            }
        }
        return root;
    }
};

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

文章讲解:代码随想录 (programmercarl.com)

题目链接:538. 把二叉搜索树转换为累加树 - 力扣(LeetCode)

题目: 给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和

递归

class Solution {
public:
    int pre = 0;
    void traversal(TreeNode* cur) {
        if (cur == nullptr) return;
        traversal(cur->right);
        cur->val += pre;
        pre = cur->val;
        traversal(cur->left);
    }
    TreeNode* convertBST(TreeNode* root) {
        pre = 0;
        traversal(root);
        return root;
    }
};

迭代

class Solution {
public:
    TreeNode* convertBST(TreeNode* root) {
        stack<TreeNode*> st;
        TreeNode* cur = root;
        int pre = 0;
        while (cur != NULL || !st.empty()) {
            if (cur) {
                st.push(cur);
                cur = cur->right;
            } else {
                cur = st.top(); st.pop();
                cur->val += pre;
                pre = cur->val;
                cur = cur->left;
            }
        }
        return root;
    }
};

总结

代码随想录 (programmercarl.com)

你可能感兴趣的:(算法,leetcode,数据结构)