struct BSTreeNode {
int m_Value;
struct BSTreeNode *m_pLeft;
struct BSTreeNode *m_pRight;
};
struct BSTreeNode *GetLowestCommonAncestor(
struct BSTreeNode *root,
struct BSTreeNode *x,
struct BSTreeNode *y)
{
struct BSTreeNOde *right, *left;
if (root == NULL) {
return NULL;
}
// 很重要的IF
if (root == x || root == y) {
return root;
}
left = GetLowestCommonAncestor(root->m_pLeft, x, y);
right = GetLowestCommonAncestor(root->m_pRight, x, y);
if (left == NULL) {
return right;
}
else if (right == NULL) {
return left;
}
else {
return root;
}
}
用递归的想法去解决是比较难理解。