【LeetCode热题100】【二叉树】将有序数组转换为二叉搜索树

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

取中间的数作为根节点,左边的数递归转换,右边的数递归转换

class Solution {
public:
    TreeNode *sortedArrayToBST(vector<int> &nums) {
        return build(nums, 0, nums.size() - 1);
    }

    TreeNode *build(vector<int> &nums, int left, int right) {
        if (left > right)
            return nullptr;
        int root = (left + right) / 2;
        return new TreeNode(nums[root], build(nums, left, root - 1), build(nums, root + 1, right));
    }
};

最近更新

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

    2024-04-10 10:34:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 10:34:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 10:34:02       82 阅读
  4. Python语言-面向对象

    2024-04-10 10:34:02       91 阅读

热门阅读

  1. SQL注入漏洞详解

    2024-04-10 10:34:02       31 阅读
  2. UML学习

    UML学习

    2024-04-10 10:34:02      29 阅读
  3. sql_mode

    2024-04-10 10:34:02       36 阅读
  4. React Router 中常用的方法总结

    2024-04-10 10:34:02       35 阅读
  5. OceanBase 中一个关于 NOT IN 子查询的 SQL 优化案例

    2024-04-10 10:34:02       25 阅读
  6. pandas习题 028:用命名元组 namedtuple 构造 DataFrame

    2024-04-10 10:34:02       32 阅读