C++ recursive, easy to understand

  • 1
    class Solution {
        TreeNode* CreateSection(vector<int>& nums,int l,int r){
            if(l>r) return NULL;
            int c = (l+r+1)/2;
            TreeNode *root = new TreeNode(nums[c]);
            root->left = CreateSection(nums,l,c-1);
            root->right = CreateSection(nums,c+1,r);
            return root;
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            return CreateSection(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.