Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3270560
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:39:05+00:00 2026-05-17T18:39:05+00:00

Hi all: i read the algorithm below to find the lowest common ancestor of

  • 0

Hi all:
i read the algorithm below to find the lowest common ancestor of two nodes in a binary search tree.

 /* A binary tree node has data, pointer to left child
   and a pointer to right child */
 struct node
 {
  int data;
  struct node* left;
  struct node* right;
 };

 struct node* newNode(int );

/* Function to find least comman ancestor of n1 and n2 */
int leastCommanAncestor(struct node* root, int n1, int n2)
{
 /* If we have reached a leaf node then LCA doesn't exist
 If root->data is equal to any of the inputs then input is
 not valid. For example 20, 22 in the given figure */
 if(root == NULL || root->data == n1 || root->data == n2)
 return -1;

 /* If any of the input nodes is child of the current node
 we have reached the LCA. For example, in the above figure
 if we want to calculate LCA of 12 and 14, recursion should
 terminate when we reach 8*/
 if((root->right != NULL) &&
  (root->right->data == n1 || root->right->data == n2))
  return root->data;
 if((root->left != NULL) &&
 (root->left->data == n1 || root->left->data == n2))
 return root->data;   

 if(root->data > n1 && root->data < n2)
   return root->data;
 if(root->data > n1 && root->data > n2)
  return leastCommanAncestor(root->left, n1, n2);
 if(root->data < n1 && root->data < n2)
  return leastCommanAncestor(root->right, n1, n2);
}    

Note that above function assumes that n1 is smaller than n2.
Time complexity: O(n)Space complexity: O(1)

this algorithm is recursive,i know that when invoking a recursive function call,the function arguments and other related registers is pushed to the stack,so extra space is needed,on the other hand, the recursive depth is related to the size or height of the tree,say n,does it make more sense to be O(n)?

Thanks for any explanations here!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:39:05+00:00Added an answer on May 17, 2026 at 6:39 pm

    Although you are right to say that that recursive implementation of the algorithm requires O(n) space because of the stack space needed, it only uses tail recursion, meaning it can be reimplemented to use O(1) space with a loop:

    int leastCommanAncestor(struct node* root, int n1, int n2)
        while (1)
        {
         /* If we have reached a leaf node then LCA doesn't exist
         If root->data is equal to any of the inputs then input is
         not valid. For example 20, 22 in the given figure */
         if(root == NULL || root->data == n1 || root->data == n2)
         return -1;
    
         /* If any of the input nodes is child of the current node
         we have reached the LCA. For example, in the above figure
         if we want to calculate LCA of 12 and 14, recursion should
         terminate when we reach 8*/
         if((root->right != NULL) &&
          (root->right->data == n1 || root->right->data == n2))
          return root->data;
         if((root->left != NULL) &&
         (root->left->data == n1 || root->left->data == n2))
         return root->data;   
    
         if(root->data > n1 && root->data < n2)
           return root->data;
         if(root->data > n1 && root->data > n2)
          root = root->left;
         else if(root->data < n1 && root->data < n2)
          root = root->right;
        }
    }
    

    (Note that else has to be added in to keep the semantics unchanged.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read that all stl containers provide a specialisation of the swap algorithm so
I have a problem. I need to trace all read/write operations to the registry
On Linux, the built-in el files are all read-only, so no problem. But on
I've set up DAV in apache2, which works great. The thing is, all read/write
I read all of Microsoft's documentation but their claim is that it should work
I read all topics related to this question in stackoverflow and whole internet and
I read all the similar questions but mine is slightly different. The first time
I read all the questions with answers but still it doesn't work for me.
I have read all the documentation I found on how to integrate Unity3D projects
I have read all threads about this without finding any answer to my problem.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.