//down-up approach for normal BTree(not a BST)
//there's no parent pointer
Node *LCA(Node *pRoot, Node *p, Node *q)
{
    if (!pRoot)
        return NULL;

    if (pRoot == p || pRoot == q)
        return pRoot;

    Node *L = LCA(pRoot->pLeft, p, q);
    Node *R = LCA(pRoot->pRight, p, q);

    if (L && R)
        return pRoot;

    return L ? L : R;
}

 

 

 

 

 

EOF

相关文章:

  • 2021-05-11
  • 2021-08-13
  • 2022-01-23
  • 2021-10-20
猜你喜欢
  • 2021-07-30
  • 2021-10-13
  • 2022-02-13
  • 2021-06-27
  • 2021-12-16
  • 2021-06-12
相关资源
相似解决方案