Binary Search Tree : Lowest Common Ancestor

  • + 2 comments

    root->(right)->x->(right)->v1->(right)->v2

    Consider this. LCA is v1's address.But since you're returning only the address without storing it in some value,it is getting lost.When control gets to x it will return it's own root ie x because each parametrised "root" is local.However storing it in root updates the value of the local root with the LCA and as the chain of hierarchy goes upwards the LCA keeps getting returned.