C++ | Sorted Array To Balanced BST | simple recursive | divide and generate


#1
TreeNode *gen(const vector<int> &A, int st, int en) {
    if(st == en)    return new TreeNode(A[st]);
    if(st > en)     return nullptr;
    int mid = (en + st) / 2;
    TreeNode *interm = new TreeNode(A[mid]);
    interm->left  = gen(A, st, mid - 1);
    interm->right = gen(A, mid + 1, en);
    return interm;
}
TreeNode* Solution::sortedArrayToBST(const vector<int> &A) {
    return gen (A, 0, A.size()-1);
}