【代码随想录】【算法训练营】【第23天】 [669]修剪二叉搜索树 [108]将有序数组转换为二叉搜索树 [538]将二叉搜索树转换为累加树

前言

思路及算法思维,指路 代码随想录。

题目来自 LeetCode。

day 23,周四,坚持~

题目详情

669 修剪二叉搜索树

题目描述

669 修剪二叉搜索树

解题思路

前提:二叉搜索树

思路:根据二叉搜索树的有序性,可以删除不在区间的结点或结点范围。

重点:删除后根节点可能会发生变化。

代码实现

C语言
搜索二叉树 递归删除
c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct TreeNode* trimBST(struct TreeNode* root, int low, int high) {
    // 判空
    if (root == NULL)
    {
        return root;
    }
    // 判断是否根节点结点
    if (root->val < low)
    {
        // 该结点及其左子树均要删除, 右子树需要递归删除
        root->right = trimBST(root->right, low, high);
        return root->right;
    }
    else if (root->val > high)
    {
        // 该结点及其右子树均要删除, 左子树需要递归删除
        root->left = trimBST(root->left, low, high);
        return root->left;
    }
    else
    {
        //根节点不修剪
        // 左子树
        root->left = trimBST(root->left, low, high);
        root->right = trimBST(root->right, low, high);
    }
    return root;
}
二叉搜索树 迭代,移动根节点使其在范围内
c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct TreeNode* trimBST(struct TreeNode* root, int low, int high) {
    // 判空
    if (root == NULL)
    {
        return root;
    }
    // 移动根节点结点,使其在[low, high]范围内
    while ((root) && ((root->val < low) || (root->val > high)))
    {
        if (root->val < low)
        {
            root = root->right;
        }
        else
        {
            root = root->left;
        }
    }
    struct TreeNode *cur = root;
    // 遍历左子树,删除小于low的元素
    while (cur)
    {
        // 移动左根节点结点,使其在[low, high]范围内
        while ((cur->left) && (cur->left->val < low))
        {
            cur->left = cur->left->right;
        }
        // 继续判断新左子树的左子树
        cur = cur->left;
    }
    // 遍历右子树,删除大于high的元素
    cur = root;
    while (cur)
    {
        // 移动右根节点结点,使其在[low, high]范围内
        while ((cur->right) && (cur->right->val > high))
        {
            cur->right = cur->right->left;
        }
        // 继续判断新右子树的右子树
        cur = cur->right;
    }
    return root;
}

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

题目描述

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

解题思路

前提:有序递增数组,转换为二叉搜索树

思路:确定根节点、左右子树的区间范围,递归遍历生成结点。

重点:升序数组与二叉搜索树的联系。

代码实现

C语言
递归
c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
    // 判空
    if (numsSize == 0)
    {
        return NULL;
    }
    // 不为空
    // 根节点
    int idx = numsSize / 2;
    struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->val = nums[idx];
    root->left = sortedArrayToBST(nums, idx);
    root->right = sortedArrayToBST(nums + idx + 1, numsSize - idx - 1);
    return root;
}
迭代 3个队列存放左右子树临界位置及根节点
c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
    // 判空
    if (numsSize == 0)
    {
        return NULL;
    }
    // 不为空
    struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    // 定义3个队列存放左边界、根节点、右边界的位置,左闭右闭
    struct TreeNode * rootQueue[10000];
    int leftQueue[10000];
    int rightQueue[10000];
    int idx = 0;
    // 队列初始化
    leftQueue[idx] = 0;
    rightQueue[idx] = numsSize - 1;
    rootQueue[idx] = root;
    idx++;
    while (idx)
    {
        struct TreeNode *cur = rootQueue[idx - 1];
        int leftLoc = leftQueue[idx - 1];
        int rightLoc = rightQueue[idx - 1];
        int rootLoc = leftLoc + (rightLoc - leftLoc) / 2;
        idx--;
        cur->val = nums[rootLoc];
        cur->left = NULL;
        cur->right = NULL;
        // 处理左区间
        if (leftLoc < rootLoc)
        {
            cur->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
            leftQueue[idx] = leftLoc;
            rightQueue[idx] = rootLoc - 1;
            rootQueue[idx] = cur->left;
            idx++;
        }
        // 处理右区间
        if (rightLoc > rootLoc)
        {
            cur->right = (struct TreeNode *)malloc(sizeof(struct TreeNode));
            leftQueue[idx] = rootLoc + 1;
            rightQueue[idx] = rightLoc;
            rootQueue[idx] = cur->right;
            idx++;
        }
    }
    return root;
}

538 将二叉搜索树转换为累加树

题目描述

538 将二叉搜索树转换为累加树

解题思路

前提:累加树,每个节点node新值>=原值之和,有点难理解什么是累加树,但是从例子中可以看书,就是该二叉搜索树中序遍历数组,从后往前相邻两值累加,覆盖原树的node值。

思路:反中序遍历,即右中左,处理node值大小。

重点:记录当前处理结点的上一节点,保留其值,用于相加。

代码实现

C语言
反中序 右中左 迭代
c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* convertBST(struct TreeNode* root) {
    // 判空
    if (root == NULL)
    {
        return root;
    }
    // 不为空树
    struct TreeNode *stack[10000];
    int idx = 0;
    struct TreeNode *cur = root;
    int pre = 0;
    while ((idx) || (cur != NULL))
    {
        if (cur)
        {
            stack[idx++] = cur;
            // 右子树
            cur = cur->right;
        }
        else
        {
            // 中
            cur = stack[--idx];
            cur->val += pre;
            pre = cur->val;
            // 左子树
            cur = cur->left;
        }
    }
    return root;
}
反中序 右中左 递归
c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

void traversal(struct TreeNode *root, int *pre)
{
    // 判空
    if (root == NULL)
    {
        return ;
    }
    // 右子树
    traversal(root->right, pre);
    // 中
    root->val += *pre;
    *pre = root->val;
    // 左子树
    traversal(root->left, pre);
    return ;
}

struct TreeNode* convertBST(struct TreeNode* root) {
    int pre = 0;
    traversal(root, &pre);
    return root;
}

今日收获

  1. 二叉搜索树的相关实现。
相关推荐
倒头就睡的小比特4 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼4 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光4 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark4 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her14 天前
并查集-听课笔记
笔记·算法·并查集
码流子4 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven4 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark4 天前
从算法设计模式看编程思维的抽象能力4
算法
2601_962218614 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法