Simple recursive yet accepted as best in cpp

  • 0
    class Solution {
        TreeNode* sortedArrayToBST(const vector<int>& nums, int l, int h)
            if(l > h) return NULL;
            int m = l+(h-l)/2;
            int val = nums[m];
            TreeNode *root = new TreeNode(val);
            root->left = sortedArrayToBST(nums, l, m-1);
            root->right = sortedArrayToBST(nums, m+1, h);
            return root;
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            return sortedArrayToBST(nums, 0, nums.size()-1);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.