Why the code doesn't work?


  • 0
    F
    void sortedArrayToBSTHelper(vector<int> &num, int start, int end, TreeNode *result)
    {
        if (end < start)
        {
            return;
        }
        int mid = (start + end) / 2;
        result = new TreeNode(num[mid]);
    
        sortedArrayToBSTHelper(num, start, mid - 1, result->left);
        sortedArrayToBSTHelper(num, mid + 1, end, result->right);
    }
    TreeNode *sortedArrayToBST(vector<int> &num) {
        TreeNode *result = NULL;
    
        sortedArrayToBSTHelper(num, 0, num.size() - 1, result);
    
        return result;
    }

  • 0
    F

    Stupid error. Passing pointer doesn't mean pass reference.


Log in to reply
 

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