You are viewing a single comment's thread. Return to all comments →
It works correctly: if v1 is an ancestor of v2, then the algorithm will traverse the tree until root is the node containing v1. At this point root.data < v1 is not true and root.data > v1 is not true, therefore both of the if-statements are bypassed and root is returned, which is the correct behavior.
can u help me to bug what is wrong with testcase 2 for my code.
If v1 is the ancestor of v2, then should the LCA = parent of v1 instead of v1?
No, if v1 is an ancestor of v2, v1 will be the lca. (this confused me for quite some time and I hope they change the challenge description to account for this.)
Agreed, it confused me too until I read your comment! Thanks :)
How can v1 be the ancestor of itself? I am still confused.
Or, are you referring to the correct answer that the question will accept?