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.

C++:
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 }
Advertisements
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s