您的位置:首页 > 其它

Lowest Common Ancestor of a Binary Search Tree (BST)

2014-12-16 23:39 465 查看
Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST.

Node* LCA(Node* root, Node* p, Node* q)
{
if (!root || !p || !q)
return NULL;
if (max(p->data, q->data) < root->data)
return LCA(root->left, p, q);
else if (min(p->data, q->data) < root->data)
return LCA(root->right, p, q);
else
return root;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: