C++ 16ms recursive solution


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

  • 0
    V

    It gives wrong answer for [1,2,3,4,5,6]


Log in to reply
 

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