-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathLowest_common_factor.cpp
35 lines (30 loc) · 1.11 KB
/
Lowest_common_factor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Lowest Common Ancestor
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int key;
Node *left; //node pointer left
Node *right;
Node (int k)
{
key=k;
left=NULL;
right=NULL;
}
};
Node *lca(Node *root,int n1, int n2)
{
if(root==NULL)
return NULL; // lca(10)
if(root->key==n1 || root->key==n2) // |--->lca(20)
return root; // | |->lca(NULL)
Node *lca1=lca(root->left,n1,n2); // | |->lca(NULL)
Node *lca2=lca(root->right,n1,n2); // |--->lca(30)
if(lca1!=NULL && lca2!=NULL) // | |->lca(40)
return root; // | |->lca(50)
if(lca1!=NULL)
return lca1;
else
return lca2;
}