forked from namishkhanna/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLowest Common Ancestor(LCA).cpp
58 lines (51 loc) · 1.03 KB
/
Lowest Common Ancestor(LCA).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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left;
Node *right;
Node(int d)
{
data=d;
left=right=NULL;
}
};
bool findpath(Node *root, vector<Node*> p,int n)
{
if(root==NULL) return false;
p.push_back(root);
if(root->data==n) return true;
if(findpath(root->left,p,n) || findpath(root->right,p,n))
return true;
p.pop_back();
return false;
}
Node* lca(Node *root, int n1, int n2)
{
vector<Node*>path1,path2;
if(findpath(root,path1,n1)==false || findpath(root,path2,n2)==false)
return NULL;
for(int i=0;i<path1.size()-1 && i<path2.size()-1; i++)
{
if(path1[i+1]!=path2[i+1])
return path1[i];
}
return NULL;
}
int main()
{
Node *root=new Node(10);
root->left=new Node(15);
root->right=new Node(20);
root->left->left=new Node(30);
root->right->left=new Node(40);
root->right->right=new Node(50);
root->right->left->left=new Node(60);
root->right->left->right=new Node(70);
//spiralorder(root);
int n1,n2;
cin>>n1>>n2;
Node *temp=lca(root,n1,n2);
cout<<"LCA:"<<temp->data<<endl;
}