这个问题有LCA算法但是我还是使用的是递归的解法,后序遍历
static bool lca(node *root, int va, int vb, node *&result, node *parent)
{
int left, right, mid;
left = right = mid = 0; //判断左右子树是否有要寻找的节点
if (root->left) left = lca(root->left, va, vb, result, root);
if (root->right) right = lca(root->right, va, vb, result, root);
//判断当前节点是否是要找的结点
if (root->data == va || root->data == vb) {
mid = 1;
}
if (!result && 2 == left + right + mid ) {
if(mid) {
result = parent;
}
result = root;
}
return left || right || mid;
}