construct a bst with minimal height

given a sorted list of number

public TreeNode constructBST( int[] array )
{
    if( array == null || array.length() <= 0 )
    {
        return null;
    }

    TreeNode root = construct( array, 0, length );
    return root;
}

public TreeNode construct( int[] arr, int low, int high )
{
    if( low >= high )
    {
        return null;
    }
    int mid = low + ( high - low ) >> 1; // aovid high+low would overflow

    TreeNode root = new TreeNode( arr[mid] );

    root.lChild = construct( arr, low, mid -1 );
    root.rChild = construct( arr, mid+1, high );
    return root;
}

Advertisements
This entry was posted in Uncategorized. 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