Skip to content

Commit e89aeaf

Browse files
committed
Lowest Common Ancestor in a Binary Tree
1 parent 8ef71f4 commit e89aeaf

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Tree Node
5+
struct Node
6+
{
7+
int data;
8+
Node* left;
9+
Node* right;
10+
};
11+
12+
// } Driver Code Ends
13+
/* A binary tree node
14+
15+
struct Node
16+
{
17+
int data;
18+
struct Node* left;
19+
struct Node* right;
20+
21+
Node(int x){
22+
data = x;
23+
left = right = NULL;
24+
}
25+
};
26+
*/
27+
28+
class Solution
29+
{
30+
public:
31+
//Function to return the lowest common ancestor in a Binary Tree.
32+
Node* lca(Node* root ,int n1 ,int n2 )
33+
{
34+
//Your code here
35+
if(!root)
36+
return NULL;
37+
if(root->data == n1 || root->data == n2)
38+
return root;
39+
Node* left = lca(root->left, n1, n2);
40+
Node* right = lca(root->right, n1, n2);
41+
if(left && right)
42+
return root;
43+
return left ? left : right;
44+
}
45+
};
46+
47+
// { Driver Code Starts.
48+
49+
Node* newNode(int val)
50+
{
51+
Node* temp = new Node;
52+
temp->data = val;
53+
temp->left = NULL;
54+
temp->right = NULL;
55+
56+
return temp;
57+
}
58+
59+
60+
// Function to Build Tree
61+
Node* buildTree(string str)
62+
{
63+
// Corner Case
64+
if(str.length() == 0 || str[0] == 'N')
65+
return NULL;
66+
67+
// Creating vector of strings from input
68+
// string after spliting by space
69+
vector<string> ip;
70+
71+
istringstream iss(str);
72+
for(string str; iss >> str; )
73+
ip.push_back(str);
74+
75+
// for(string i:ip)
76+
// cout<<i<<" ";
77+
// cout<<endl;
78+
// Create the root of the tree
79+
Node* root = newNode(stoi(ip[0]));
80+
81+
// Push the root to the queue
82+
queue<Node*> queue;
83+
queue.push(root);
84+
85+
// Starting from the second element
86+
int i = 1;
87+
while(!queue.empty() && i < ip.size()) {
88+
89+
// Get and remove the front of the queue
90+
Node* currNode = queue.front();
91+
queue.pop();
92+
93+
// Get the current node's value from the string
94+
string currVal = ip[i];
95+
96+
// If the left child is not null
97+
if(currVal != "N") {
98+
99+
// Create the left child for the current node
100+
currNode->left = newNode(stoi(currVal));
101+
102+
// Push it to the queue
103+
queue.push(currNode->left);
104+
}
105+
106+
// For the right child
107+
i++;
108+
if(i >= ip.size())
109+
break;
110+
currVal = ip[i];
111+
112+
// If the right child is not null
113+
if(currVal != "N") {
114+
115+
// Create the right child for the current node
116+
currNode->right = newNode(stoi(currVal));
117+
118+
// Push it to the queue
119+
queue.push(currNode->right);
120+
}
121+
i++;
122+
}
123+
124+
return root;
125+
}
126+
127+
// Function for Inorder Traversal
128+
void printInorder(Node* root)
129+
{
130+
if(!root)
131+
return;
132+
133+
printInorder(root->left);
134+
cout<<root->data<<" ";
135+
printInorder(root->right);
136+
}
137+
138+
int main() {
139+
int t;
140+
scanf("%d",&t);
141+
while(t--)
142+
{
143+
int a,b;
144+
scanf("%d %d ",&a,&b);
145+
string s;
146+
getline(cin,s);
147+
Node* root = buildTree(s);
148+
Solution ob;
149+
cout<<ob.lca(root,a,b)->data<<endl;
150+
}
151+
return 0;
152+
}
153+
// } Driver Code Ends

Some_More_Trees/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@
5050
/ \
5151
3 2
5252
Output: 3 1 2
53-
Explanation: First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2.
53+
Explanation: First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2.
54+
55+
## 4. Lowest Common Ancestor in a Binary Tree:
56+
Given a Binary Tree with all unique values and two nodes value n1 and n2. The task is to find the lowest common ancestor of the given two nodes. We may assume that either both n1 and n2 are present in the tree or none of them is present.
57+
Example 1:
58+
Input: n1 = 2 , n2 = 3
59+
1
60+
/ \
61+
2 3
62+
Output: 1
63+
Explanation: LCA of 2 and 3 is 1.

0 commit comments

Comments
 (0)