代码随想录第21天:669. 修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树

669. 修剪二叉搜索树

// 669. 修剪二叉搜索树
#include 
#include 

using namespace std;

struct TreeNode

{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

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

void printPreorder(TreeNode *node)
{
    if (node == nullptr)
    {
        return; // 如果节点为空,则返回
    }

    std::cout << node->val << " "; // 首先打印根节点的值
    printPreorder(node->left);     // 递归打印左子树
    printPreorder(node->right);    // 递归打印右子树
}

int main()
{
    TreeNode *t2 = new TreeNode(3);
    t2->left = new TreeNode(0);
    t2->left->right = new TreeNode(2);
    t2->left->right->left = new TreeNode(1);
    t2->right = new TreeNode(4);

    TreeNode *res = trimBST(t2, 1, 3);
    printPreorder(res);

    return 0;
}

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

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

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

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

void printPreorder(TreeNode *node)
{
    if (node == nullptr)
    {
        return; // 如果节点为空,则返回
    }

    std::cout << node->val << " "; // 首先打印根节点的值
    printPreorder(node->left);     // 递归打印左子树
    printPreorder(node->right);    // 递归打印右子树
}

int main()
{
    vector nums = {-10, -3, 0, 5, 9};

    TreeNode *res = sortedArrayToBST(nums);
    printPreorder(res);

    return 0;
}

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

// 538.把二叉搜索树转换为累加树, 累加树就是把数组中比自己大的元素进行求和并更新
#include 
#include 

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

int pre = 0; // 记录前一个节点的数值
void traversal(TreeNode *cur)
{ // 右中左遍历
    if (cur == NULL)
        return;
    traversal(cur->right);
    cur->val += pre;
    pre = cur->val;
    traversal(cur->left);
}

TreeNode *convertBST(TreeNode *root)
{
    pre = 0;
    traversal(root);
    return root;
}

void printPreorder(TreeNode *node)
{
    if (node == nullptr)
    {
        return; // 如果节点为空,则返回
    }

    std::cout << node->val << " "; // 首先打印根节点的值
    printPreorder(node->left);     // 递归打印左子树
    printPreorder(node->right);    // 递归打印右子树
}

int main()
{
    TreeNode *t1 = new TreeNode(1);
    t1->left = new TreeNode(0);
    t1->right = new TreeNode(2);

    TreeNode *res = convertBST(t1);
    printPreorder(res);

    return 0;
}

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