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

669. 修剪二叉搜索树

题目链接

669. 修剪二叉搜索树 - 力扣(LeetCode)

思路

跟删除节点的方式是差不多的。

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;
    }
};

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

题目链接

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

思路

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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 00:26:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 00:26:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 00:26:03       82 阅读
  4. Python语言-面向对象

    2024-03-15 00:26:03       91 阅读

热门阅读

  1. 25.MySQL中定义空值

    2024-03-15 00:26:03       40 阅读
  2. Flutter Widgets体系结构

    2024-03-15 00:26:03       54 阅读
  3. 探讨:MySQL和PostgreSQL谁更火

    2024-03-15 00:26:03       44 阅读
  4. python实现小红树上染色

    2024-03-15 00:26:03       38 阅读
  5. ubuntu20.04安装fpylll

    2024-03-15 00:26:03       38 阅读
  6. SQL Server 技术100问?

    2024-03-15 00:26:03       42 阅读
  7. 网络模型的保存和读取

    2024-03-15 00:26:03       38 阅读