In a general tree, how would you find the lowest common ancestor of two nodes that are given to you as parameters?
tree* foo(tree* root, tree* n1, tree* n2)
{
if(!root) return NULL;
if(root==n1||root==n2) return root;
tree* left=foo(root->left);
tree* right=foo(root->right);
if(left&&right) return root;
return left?left:right;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment