Some error occured while loading page for you. Please try again.

We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

the second loop should be checking if the cur is less than v1 and v2 because if we had 8 , 11, 10, 12, 9, 13 and v1 = 9, v2 =13 your loops would return 8 which is wrong.

## Binary Search Tree : Lowest Common Ancestor

You are viewing a single comment's thread. Return to all comments →

Thanks, that was really helpful. This is my C++ Solution

Took a second, but I get it. It only actually uses one of the loops. It also doesn't need to care how v1 and v2 are ordered.

No need to obfuscate it, though.

A bit confusing for this and I found a counter example.

I had a tree: 8 4 9 1 6 2 5 7 3

lca of 2 and 3 should be 2, but this logic return 1

This is your tree. The answer code runs fine.

Hi, shouldn't the tree be like this:

No, for a tree: 8 4 9 1 6 2 5 7 3. It should look like this: (forgiven me about my bad indentation)

Tree flase

what if current->data>v1&¤t->data

This is wrong for the following input:

also wrong for below output 7 4 2 7 1 3 6 8 6 8

the second loop should be checking if the cur is less than v1 and v2 because if we had 8 , 11, 10, 12, 9, 13 and v1 = 9, v2 =13 your loops would return 8 which is wrong.