Simple c++ solution


  • 0
    I
    TreeNode* help(int left, int right, vector<int>& nums) {
        if (left == right) return new TreeNode(nums[left]);
        int mid = (left + right) / 2;
        TreeNode *root = new TreeNode(nums[mid]);
        if (left < mid) root->left = help(left, mid - 1, nums);
        if (mid < right) root->right = help(mid + 1, right, nums);
        return root;
    }
    
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        if (nums.empty()) return NULL;
        return help(0, nums.size() - 1, nums);
    }

Log in to reply
 

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