「代码随想录算法训练营」第十八天 | 二叉树 part8

669. 修剪二叉搜索树

题目链接:https://leetcode.cn/problems/trim-a-binary-search-tree/
题目难度:中等
文章讲解:https://programmercarl.com/0669.修剪二叉搜索树.html
视频讲解:https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web
题目状态:没有思路,看题解过

思路:

使用递归。

  1. 参数和返回值:参数是节点,和左右边界,返回值是节点。
  2. 终止条件:当节点为nullptr时,终止。
  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* trimBST(TreeNode* root, int low, int high) {
        if(root == nullptr) return nullptr;
        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;
    }
};

迭代法代码:

/**
 * 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;
        while(root != nullptr && (root->val < low || root->val > high)) {
            if(root->val < low) root = root->right;
            else root = root->left;
        }
        TreeNode *cur = root;
        while(cur != nullptr) {
            while(cur->left && cur->left->val < low) cur->left = cur->left->right;
            cur = cur->left;
        }
        cur = root;
        while(cur != nullptr) {
            while(cur->right && cur->right->val > high) cur->right = cur->right->left;
            cur = cur->right;
        }
        return root;
    }
};

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

题目链接:https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/
题目难度:简单
文章讲解:https://programmercarl.com/0108.将有序数组转换为二叉搜索树.html
视频讲解:https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web
题目状态:依旧是看题解

思路:

以数组的中间为根结点,分别截取数组的左半部分作为根结点的左孩子,截取数组的右半部分作为根结点的右孩子。

代码:

/**
 * 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* 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) {
        TreeNode *root = traversal(nums, 0, nums.size() - 1);
        return root;
    }
};

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

题目链接:https://leetcode.cn/problems/convert-bst-to-greater-tree/
题目难度:中等
文章讲解:https://programmercarl.com/0538.把二叉搜索树转换为累加树.html
视频讲解:https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web
题目状态:依旧是看题解

思路:

通过一个int类型的全局变量来保存比当前节点大的节点的值,并使用右中左的顺序来遍历二叉树。

代码:

/**
 * 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:
    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;
    }
};

迭代法:

/**
 * 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:
    int pre;
    void traversal(TreeNode* root) {
        stack<TreeNode*> st;
        TreeNode* cur = root;
        while(cur != nullptr || !st.empty()) {
            if(cur != nullptr) {
                st.push(cur);
                cur = cur->right;
            } else {
                cur = st.top();
                st.pop();
                cur->val += pre;
                pre = cur->val;
                cur = cur->left;
            }
        }
    }
    TreeNode* convertBST(TreeNode* root) {
        pre = 0;
        traversal(root);
        return root;
    }
};

热门相关:女王嫁到:老公,太凶了   从港综签到成为传说   自古红楼出才子   脑洞大爆炸   你是我生命的四分之三