Binary Search Tree : Lowest Common Ancestor

  • + 0 comments

    It is better to first check if you have already found the lce and then decide which path to go.

    To, the same idea without brackets would be like this:

    node * lca(node * root, int v1,int v2)
    {
       int diff1 = root->data - v1, diff2 = root->data - v2;
       if (diff1 * diff2 <= 0) return root;
       if (diff1 < 0) return lca(root->right, v1, v2);
       return lca(root->left, v1, v2);
    }