edited by
431 views
5 votes
5 votes

What is the best time complexity (not the best case input but the best algorithm which will work for any input) for finding the lowest common ancestor for two keys $a$ and $b$ in a binary search tree of $n$ nodes and height $h$?

  1. $\Theta(n)$
  2. $\Theta(n \log n)$
  3. $O(h)$
  4. $\Omega(n)$
edited by

1 Answer

Best answer
10 votes
10 votes

We can do a recursive function as follows:

struct node * find(struct node *root,int  val1, int val2)
{
   if(!root) return NULL;
   if(root->val > val1 && root -> val > val2)
   {
      return  find(root->left, val1, val2);
   }
   if(root->val < val1 && root -> val < val2)
   {
        return find(root->right, val1, val2);
   }
   return root;
}
selected by
Answer:

Related questions

3 votes
3 votes
1 answer
3
gatecse asked Aug 9, 2020
227 views
The cut vertices in the given graph areE and FA, C and DC, E and FB and C
1 votes
1 votes
1 answer
4