You are viewing a single comment's thread. Return to all comments →
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.
node * lca(node * root, int v1,int v2) { node *cur{root}; while (cur->data > v1 && cur->data > v2) cur = cur->left; while (cur->data > v1 && cur->data > v2) cur = cur->right; return cur; }
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
8 / \ 4 9 / \ 1 6 / \ 3 2
This is your tree. The answer code runs fine.
Hi, shouldn't the tree be like this:
8 / \ 4 9 / \ 1 6 \ 2 \ 3
No, for a tree: 8 4 9 1 6 2 5 7 3. It should look like this: (forgiven me about my bad indentation)
8 / \ 4 9 / \ 1 6 \ / \ 2 5 7 \ 3
Tree flase
what if current->data>v1&¤t->data
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Binary Search Tree : Lowest Common Ancestor
You are viewing a single comment's thread. Return to all comments →
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