C++ 13ms recursion


  • 0
    class Solution {
    public:
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            if(nums.empty()) return NULL;
            TreeNode* root = new TreeNode(nums[nums.size() / 2]);
            vector<int>left(nums.begin(), nums.begin() + nums.size() / 2);
            vector<int>right(nums.begin() + nums.size() / 2 + 1, nums.end());
            root->left = sortedArrayToBST(left);
            root->right = sortedArrayToBST(right);
            return root;
        }
    };
    

Log in to reply
 

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