LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  TreeNode* sortedArrayToBST(vector<int>& nums) {
    return dfs(nums, 0, size(nums));
  }

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