#include <iostream>
using namespace std;
struct node{ int data;
struct node* left;
struct node*right;
};
struct node* createnode(int num)
{
struct node* p=(struct node*)malloc(sizeof(struct node));
p->data=num;
p->left=NULL;
p->right=NULL;
return p;
}
struct node* findlca(struct node*root,int n1,int n2)
{
if(root==NULL || root->data==n1 || root->data==n2)
return root;
struct node* leftlca=findlca(root->left,n1,n2);
struct node* rightlca=findlca(root->right,n1,n2);
if(leftlca && rightlca)
return root;
if(leftlca!=NULL)
return (leftlca);
else
return(rightlca);
}
void preordertraversal(struct node* root)
{
if(root==NULL)
return;
printf("%d" ,root->data);
preordertraversal(root->left);
preordertraversal(root->right);
}
void inordertraversal(struct node* root)
{
if(root==NULL)
return;
inordertraversal(root->left);
printf("%d" ,root->data);
inordertraversal(root->right);
}
void postordertraversal(struct node* root)
{
if(root==NULL)
return;
postordertraversal(root->left);
postordertraversal(root->right);
printf("%d" ,root->data);
}
int main() {
struct node* root=createnode(5);
root->left=createnode(2);
root->left->left=createnode(1);
root->left->right=createnode(3);
root->right=createnode(7);
root->right->left=createnode(6);
root->right->right=createnode(8);
struct node* tree =findlca(root,1,8);
printf("%d ",tree->data);
printf("\n");
preordertraversal(root);
printf("\n");
inordertraversal(root);
printf("\n");
postordertraversal(root);
return 0;
}