static bool lca(Node *root, int va, int vb, Node *&result, Node* parrent)
{
// left/right 左/右子树是否含有要判断的两节点之一
bool left = false, right = false;
if (!result && root->left) left = lca(root->left,va,vb,result,root);
if (!result && root->right) right = lca(root->right,va,vb,result,root);
// mid 当前节点是否是要判断的两节点之一
bool mid = false;
if (root->data == va || root->data == vb) mid=true;
if (!result && int(left + right + mid) == 2) {
if (mid) result = parrent;
else result = root;
}
return left | mid | right ;
}
Node *lca(Node *root,int va, int vb)
{
if (root == NULL) return NULL;
Node *result = NULL;
lca(root, va, vb,result, NULL);
return result;
}
|