Find recent public ancestor
The premise of finding is that node A and Node B are all in the tree.
The main action object is a two-fork search tree, the following steps: Remember the current root node is x, if a < X and B > x (or vice versa), then X is the required ancestor; If a, B is less than (greater than) x, the description also continues to look to the left (right) subtree, if x = a (b) b) is the ancestor of B (a).
/* Find common ancestor *
/int findAncestor (node* root, int a, int b)
{
//b node at the left and right side of the root node, indicating that the root node is the common ancestor
if (a < ROOT-&G T;data && B > Root->data | | A > Root->data && B < root->data)
return root->data;
A (b) is the ancestor of B (a)
if (Root->data = = A | | root->data = = b)
return root->data;
Continue to the left and right subtree to find
if (a < Root->data && B < root->data)
return FindRoot (Root->lchild, A, b); C12/>else if (a > root->data && B > Root->data)
return FindRoot (Root->rchild, A, b);
}