Reconstruct BST using preorder traversal.

Given preorder traversal array of a BST, recontruct the BST.

C++:
01 Node* constructBST(int A[], int& start, int max, int end)
02 {
03     if((start>end)||(A[start]>max))
04         return NULL;
05     int data=A[start++];
06     Node* enode=new Node;
07     enode->value=data;
08     enode->left=constructBST(A,start,data,end);
09     enode->right=constructBST(A,start,max,end);
10     return enode;
11 }
Advertisements
This entry was posted in Recursive, 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