checking if an array is a post order of BST

recursive :

boolean isPostOrder( int a[], int length )
{
    int root = a[length - 1];
    if( length <= 1 )
    {
        return true;
    }
    int i, j;
    for( i = 0; i < length -1 && a[i] < root; i++ );

    for( j = i; j < length -1 ; j ++ )
    {
        if( a[j] < root )
        {
            return false;
        }
    }

    return isPostOrder( a, i ) && isPostOrder( a+i, n-1-i );

}

Advertisements
This entry was posted in interview and tagged . 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