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* constructMaximumBinaryTree(vector<int>& nums) {
    return constructMaximumBinaryTree(nums, 0, nums.size());
  }

  TreeNode* constructMaximumBinaryTree(vector<int>& nums, int l, int r) {
    if (l == r) {
      return nullptr;
    }
    int max_index =
        max_element(nums.begin() + l, nums.begin() + r) - nums.begin();
    TreeNode* root = new TreeNode(nums[max_index]);
    root->left = constructMaximumBinaryTree(nums, l, max_index);
    root->right = constructMaximumBinaryTree(nums, max_index + 1, r);
    return root;
  }
};