Binary Search Tree : Lowest Common Ancestor

  • + 2 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;    
    }