My accepted 16ms simple C++ solution


  • 0
    J
    class Solution {
        TreeNode* sArray2BST(vector<int>& nums, int low, int high)
        {
            if (low > high) return NULL;
            int mid = low + ceil(float(high-low)/2.0);
            TreeNode *head = new TreeNode(nums[mid]);
            head->left = sArray2BST(nums, low, mid-1);
            head->right = sArray2BST(nums, mid+1, high);
            return head;
        }
    public:
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            if (nums.size() == 0) return NULL;
            return sArray2BST(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.