函数原型定义如下:
Node *getLCA(Node* root, int n1, int n2)
其中n1和n2是指定的两个节点值。

下面是来自Wikipedia的关于LCA的定义:
假设存在一颗二叉树T, 其中节点n1和n2的最小共同祖先的定义为,T中最小的节点,它包含了n1和n2做为后代(同时规定一个节点自己可以做为自己的后代).
节点n1和n2的LCA是一个共同的祖先,距离root根节点最远。对于LCA进行计算是很有用的,例如,可以计算树中一对节点之间的距离:n1到n2的距离为n1到root的距离,加上n2的root的距离,再减去它们的LCA到root距离的两倍。
// C++程序,求BST这两个节点的LCA
#include <iostream>
struct Node
{
int key;
Node *left;
Node *right;
};
//求n1和n2的LCA。假设n1和n2都位于BST中。
Node *getLCA(Node* root, int n1, int n2)
{
if (root == NULL)
return NULL;
// 如果n1和n2小于root,则LCA位于左子树中
if (root->key > n1 && root->key > n2)
return getLCA(root->left, n1, n2);
// 如果n1和n2大于root,则LCA位于右子树中
if (root->key < n1 && root->key < n2)
return getLCA(root->right, n1, n2);
return root;
}
// 创建一个新的BST节点
Node *createNewNode(int item)
{
Node *temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
int main()
{
/*
20
/ 8 22
/ \
4 12
/ 10 14
*/
Node *root = createNewNode(20);
root->left = createNewNode(8);
root->right = createNewNode(22);
root->left->left = createNewNode(4);
root->left->right = createNewNode(12);
root->left->right->left = createNewNode(10);
root->left->right->right = createNewNode(14);
int n1 = 10, n2 = 14;
Node *t = getLCA(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->key);
n1 = 14, n2 = 8;
t = getLCA(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->key);
n1 = 10, n2 = 22;
t = getLCA(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->key);
return 0;
}输出://求节点n1和n2的LCA
Node *getLCA(Node* root, int n1, int n2)
{
while (root != NULL)
{
// 如果n1和n2小于root,则LCA位于左子树中
if (root->data > n1 && root->data > n2)
root = root->left;
// 如果n1和n2大于root,则LCA位于右子树中
else if (root->data < n1 && root->data < n2)
root = root->right;
else break;
}
return root;
}原文地址:http://blog.csdn.net/shltsh/article/details/46510599