-
Notifications
You must be signed in to change notification settings - Fork 0
/
lca.cpp
112 lines (106 loc) · 2.35 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<iostream>
#include<climits>
#include<vector>
#include<queue>
using namespace std;
class Node{
public:
int data;
Node * left;
Node * right;
Node(int val): data(val),left(NULL), right(NULL){};
Node (): data(0), left(NULL), right(NULL){};
};
void buildTree(int * arr, int size, Node * nodeArr){
for(int i=0; i<size; i++){
if(arr[i]!=0){
if(i==0){
nodeArr[0].data= arr[i];
}
else{
nodeArr[i].data = arr[i];
int parentPos= (i-1)/2;
//Node parent = nodeArr[parentPos];
if(i%2 == 1)
nodeArr[parentPos].left=&nodeArr[i];
else
nodeArr[parentPos].right=&nodeArr[i];
}
}
}
return ;
}
void PrintLineOrder(Node * root){
if(root == NULL) return;
queue<Node*> currentLevel, nextLevel;
currentLevel.push(root);
while(!currentLevel.empty()){
Node * curNode = currentLevel.front();
currentLevel.pop();
if(curNode){
cout << curNode->data << " ";
nextLevel.push(curNode->left);
nextLevel.push(curNode->right);
}
if(currentLevel.empty()){
cout << "\n";
queue<Node*> temp = currentLevel;
currentLevel=nextLevel;
nextLevel = temp;
}
}
}
bool findPath(Node * node,int val, vector<int> &path){
if(node->data == val) {
path.push_back(node->data);
return true;
}
if(node == NULL) return false;
path.push_back(node->data);
if(val<node->data){
node=node->left;
}
else{
node = node->right;
}
return findPath(node, val, path);
}
void lcaNode(Node * node, int val1, int val2){
vector<int> path1, path2;
if(findPath(node, val1, path1)){
if(findPath(node, val2, path2)){
for(int i =0; i<path1.size();i++){
if(path1[i] != path2[i]) {
cout << "FindLCA(" << val1 << ","<< val2<< ")";
cout << path1[i-1] << endl;
return;
}
}
}
else{
cout << "No LCA value for (" << val1 << ","<< val2<< ")";
}
}
else{
cout << "No LCA value for (" << val1 << ","<< val2<< ")";
}
return;
}
int main(){
int n;
int num;
cin>> n;
int inputArr[n];
for (int i =0; i<n; i++){
cin>> num;
inputArr[i]=num;
}
Node root[n];
buildTree(inputArr, n, root);
PrintLineOrder(root);
cout << endl;
lcaNode(root, 10, 20);
lcaNode(root, 50, 80);
lcaNode(root, 20, 60);
return 0;
}