Saturday, 11 January 2014

Find Lowest Common Ancestor in BST

node *lca(node *root, int n1, int n2){
    if (!root) return NULL;
    if (root->data > n1 && root->data > n2)
        return lca(root->left, n1, n2);
    if (root->data < n1 && root->data < n2)
        return lca(root->right, n1, n2);
    return root;
}

No comments:

Post a Comment