Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
public static node find( node *root)
{
if( root == null)
return null;
else
if(root->value > first && root->value<second)
return root;
else
if(root->value <first)
find(root->right);
else
if(root->value>right)
find(root->left);
}
To down approach. While visiting a node, check if the node is equal to any of the given input numbers. If yes terminate and return the node. If the node value is in between the given input numbers that is the LCA, return the node and terminate. Otherwise
if node value is less than lower number then recur in the right sub tree.
if node value is greater than the higher input then recur in the left sub tree.
Code below,
- GeeBee September 12, 2011