Convert sorted array to balanced binary search tree (BST)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

Sponsored: has published a chatbot, who can chat with you and help you find apartments in San Francisco and Bay area. Check it out:

01 Node* buildArrayBST(int *A, int size, int start, int end)
02 {
03     if(start>end)
04         return NULL;
05     int mid=(start+end)/2;
06     Node* head=new Node;
07     head->value=A[mid];
08     head->left=buildArrayBST(A,size,start,mid1);
09     head->right=buildArrayBST(A,size,mid+1,end);
10     return head;
11 }
This entry was posted in Array and linked list, Divide and Conquer, Tree. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s