13ms cpp solution


  • 0
    R
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return makeTree(nums, 0, nums.size());
    }
    
    TreeNode* makeTree(vector<int>& nums, int s, int e) {
        if (s >= e) {
            return NULL;
        }
        int m = (e + s)/2;
        TreeNode* root = new TreeNode(nums[m]);
        root->left = makeTree(nums, s, m);
        root->right = makeTree(nums, m +1, e);
        return root;
    }

  • 0
    R
    This post is deleted!

Log in to reply
 

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