0

this is my implementation of my binary tree (this is not the code for Lca just normal implementation of binary tree just to give a idea how i constructed the binary tree)

void insert(int n)
{
create(&root,n);
}
void create(node** temp,int n)
{
if(root==NULL)
{
(*temp)=new node;
(*temp)->data=n;
(*temp)->right=NULL;
(*temp)->left=NULL;
root=*temp;
}
else
{
if((*temp)==NULL)
{
(*temp)=new node;
(*temp)->data=n;
(*temp)->right=NULL;
(*temp)->left=NULL;
}
else
{
char c;
cout<<"enter the direction";
cout<<endl;
cin>>c;
if(c=='l')
create(&((*temp)->left),n);
else
create(&((*temp)->right),n);
}
}
}

now my question is how to find lowest common ancestor of two nodes if the two nodes are same in both right subtree and left subtree for example lca of 4 and 5 see that 4 and 5 are there in both right sub tree and left sub tree of root(two times ocurred)

so what should be lowest common ancestor of this

i understood the answer of nick johnson in the below question but i am not understanding how to do the above type of tree

How to find the lowest common ancestor of two nodes in any binary tree?

Community
  • 1
  • 1
user2416871
  • 533
  • 1
  • 4
  • 13

0 Answers0