Simple C++ solution recursive

/**

  • Definition for binary tree
  • struct TreeNode {
  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    */

TreeNode create(vector &A,int start,int end)
{
if(start==end)
return NULL;
int middle=(start+end)/2;
TreeNode
temp=(TreeNode*)malloc(sizeof(TreeNode));
temp->val=A[middle];
temp->left=create(A,start,middle);
temp->right=create(A,middle+1,end);
return temp;
}
TreeNode* Solution::sortedArrayToBST(const vector &A) {
int n=A.size();
vector B=A;
return(create(B,0,n));
}

Click here to start solving coding interview questions