-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowestCommonAncestorOfBinaryTree.cpp
154 lines (149 loc) · 3.67 KB
/
lowestCommonAncestorOfBinaryTree.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Implemented by Kritagya Kumra
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
// void solve(node *root, vector<int> &ans, int endEle)
// {
// if (root == NULL)
// {
// return;
// }
// if (root->data == endEle)
// {
// ans.push_back(root->data);
// return;
// }
// ans.push_back(root->data);
// solve(root->left, ans, endEle);
// solve(root->right, ans, endEle);
// }
// vector<int> getLevelOrder(node *root, int endEle)
// {
// vector<int> ans;
// solve(root, ans, endEle);
// return ans;
// }
// node *lca(node *root, int n1, int n2)
// {
// vector<int> pathToN1 = getLevelOrder(root, n1);
// vector<int> pathToN2 = getLevelOrder(root, n2);
// for (int i = 0; i < pathToN1.size(); i++)
// {
// cout << pathToN1[i] << " ";
// }
// for (int i = 0; i < pathToN2.size(); i++)
// {
// cout << pathToN2[i] << " ";
// }
// return NULL;
// }
void traverse(node *root, int n1, int n2, vector<node *> &list1, vector<node *> &list2)
{
if (root == NULL)
return;
list1.push_back(root);
list2.push_back(root);
traverse(root->left, n1, n2, list1, list2);
traverse(root->right, n1, n2, list1, list2);
if (list1.back()->data != n1)
list1.pop_back();
if (list2.back()->data != n2)
list2.pop_back();
}
// Function to return the lowest common ancestor in a Binary Tree.
node *lca(node *root, int n1, int n2)
{
if (root == NULL)
return NULL;
vector<node *> list1;
vector<node *> list2;
traverse(root, n1, n2, list1, list2);
while (list1.back() != list2.back())
{
if (list1.size() > list2.size())
list1.pop_back();
else if (list1.size() < list2.size())
list2.pop_back();
else
{
list1.pop_back();
list2.pop_back();
}
}
return list1.back();
}
node *lca1(node *root, int n1, int n2)
{
if (root == NULL)
{
return NULL;
}
if (root->data == n1 || root->data == n2)
{
return root;
}
node *left = lca1(root->left, n1, n2);
node *right = lca1(root->right, n1, n2);
if (left == NULL && right == NULL)
{
return NULL;
}
else if (left != NULL && right == NULL)
{
return left;
}
else if (right != NULL && left != NULL)
{
return root;
}
else
{
return right;
}
}
int main()
{
node *root1 = new node(10);
root1->left = new node(8);
root1->right = new node(2);
root1->left->left = new node(3);
root1->left->right = new node(5);
root1->right->left = new node(22);
root1->right->left->right = new node(12);
root1->right->left->right->left = new node(11);
// node *root1 = new node(20);
// root1->left = new node(8);
// root1->right = new node(12);
// root1->left->left = new node(3);
// root1->left->right = new node(5);
// root1->right->left = new node(2);
// root1->right->right = new node(10);
node *root = new node(1);
root->left = new node(2);
root->right = new node(3);
root->left->left = new node(4);
root->left->right = new node(5);
root->right->left = new node(6);
root->right->right = new node(7);
root->right->left->right = new node(8);
root->right->right->right = new node(9);
node *ans = lca(root, 4, 5);
cout << "The sum Of Longest Root To Leaf Path is " << ans->data;
return 0;
}
// Implemented by Kritagya Kumra