Wednesday, September 12, 2007

Solution:
------------

let a and b be given nos ( or values of given nodes )

common_ance( node )
{
........if( ( a <node && b > node ) || ( a > node && b < node ) || a == node || b== node)
........ return node

........else if ( a < node && b < node )
........ return common_ance( node - > left )

........ else
........ return common_ance ( node -> right )
}

initial call is common_ance( root )

Psudocode:
----------------

first condition checks if given nodes lie in left n right subtrees of root
if yes return root
else
if both lie in left subtree then call common_ance( root -> left )
else common_ance ( root - > right )


Powered by ScribeFire.

No comments:

Post a Comment