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

  • Home
  • SEARCH
  • 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

We've all read the benchmarks and know the facts - event-based asynchronous network servers
I read all over the Internet (various sites and blogs) about version control. How
I read all over the place about how Spring encourages you to use interfaces
I've read all the advice on const-correctness in C++ and that it is important
I want to read all items of a feed in C#. The solutions I've
I'm using sockets to read all incoming SMS : `smsAddr.SetTextMatch(KNullDesC8);` The ones that belong
how can i create an application to read all my browser (firefox) history? i
I have this function to read in all ints from the file. The problem
All articles I've read about localization of a WinForms application assume that I already
I read an interesting DailyWTF post today, Out of All The Possible Answers... and

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.