Lowest Common Ancestor in Parent Array Representation
Given a binary tree represented as parent array, find Lowest Common Ancestor between two nodes ‘m’ and ‘n’.
In the above diagram, LCA of 10 and 14 is 12 and LCA of 10 and 12 is 12.
(1) Make a parent array and store the parent of ith node in it. Parent of root node should be -1.
(2) Now, access all the nodes from the desired node ‘m’ till root node and mark them visited.
(3) Lastly, access all the nodes from the desired node ‘n’ till first visited node comes.
(4) This node is the lowest common ancestor
C++
// CPP program to find LCA in a tree represented // as parent array. #include <bits/stdc++.h> using namespace std; // Maximum value in a node const int MAX = 1000; // Function to find the Lowest common ancestor int findLCA( int n1, int n2, int parent[]) { // Create a visited vector and mark // all nodes as not visited. vector< bool > visited(MAX, false ); visited[n1] = true ; // Moving from n1 node till root and // mark every accessed node as visited while (parent[n1] != -1) { visited[n1] = true ; // Move to the parent of node n1 n1 = parent[n1]; } visited[n1] = true ; // For second node finding the first // node common while (!visited[n2]) n2 = parent[n2]; return n2; } // Insert function for Binary tree void insertAdj( int parent[], int i, int j) { parent[i] = j; } // Driver Functiom int main() { // Maximum capacity of binary tree int parent[MAX]; // Root marked parent[20] = -1; insertAdj(parent, 8, 20); insertAdj(parent, 22, 20); insertAdj(parent, 4, 8); insertAdj(parent, 12, 8); insertAdj(parent, 10, 12); insertAdj(parent, 14, 12); cout << findLCA(10, 14, parent); return 0; } |
Java
// Java program to find LCA in a tree represented // as parent array. import java.util.*; class GFG { // Maximum value in a node static int MAX = 1000 ; // Function to find the Lowest common ancestor static int findLCA( int n1, int n2, int parent[]) { // Create a visited vector and mark // all nodes as not visited. boolean []visited = new boolean [MAX]; visited[n1] = true ; // Moving from n1 node till root and // mark every accessed node as visited while (parent[n1] != - 1 ) { visited[n1] = true ; // Move to the parent of node n1 n1 = parent[n1]; } visited[n1] = true ; // For second node finding the first // node common while (!visited[n2]) n2 = parent[n2]; return n2; } // Insert function for Binary tree static void insertAdj( int parent[], int i, int j) { parent[i] = j; } // Driver Functiom public static void main(String[] args) { // Maximum capacity of binary tree int []parent = new int [MAX]; // Root marked parent[ 20 ] = - 1 ; insertAdj(parent, 8 , 20 ); insertAdj(parent, 22 , 20 ); insertAdj(parent, 4 , 8 ); insertAdj(parent, 12 , 8 ); insertAdj(parent, 10 , 12 ); insertAdj(parent, 14 , 12 ); System.out.println(findLCA( 10 , 14 , parent)); } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 program to find LCA in a # tree represented as parent array. # Maximum value in a node MAX = 1000 # Function to find the Lowest # common ancestor def findLCA(n1, n2, parent): # Create a visited vector and mark # all nodes as not visited. visited = [ False for i in range ( MAX )] visited[n1] = True # Moving from n1 node till root and # mark every accessed node as visited while (parent[n1] ! = - 1 ): visited[n1] = True # Move to the parent of node n1 n1 = parent[n1] visited[n1] = True # For second node finding the # first node common while (visited[n2] = = False ): n2 = parent[n2] return n2 # Insert function for Binary tree def insertAdj(parent, i, j): parent[i] = j # Driver Code if __name__ = = '__main__' : # Maximum capacity of binary tree parent = [ 0 for i in range ( MAX )] # Root marked parent[ 20 ] = - 1 insertAdj(parent, 8 , 20 ) insertAdj(parent, 22 , 20 ) insertAdj(parent, 4 , 8 ) insertAdj(parent, 12 , 8 ) insertAdj(parent, 10 , 12 ) insertAdj(parent, 14 , 12 ) print (findLCA( 10 , 14 , parent)) # This code is contributed by # Surendra_Gangwar |
C#
// C# program to find LCA in a tree represented // as parent array. using System; class GFG { // Maximum value in a node static int MAX = 1000; // Function to find the Lowest common ancestor static int findLCA( int n1, int n2, int []parent) { // Create a visited vector and mark // all nodes as not visited. Boolean []visited = new Boolean[MAX]; visited[n1] = true ; // Moving from n1 node till root and // mark every accessed node as visited while (parent[n1] != -1) { visited[n1] = true ; // Move to the parent of node n1 n1 = parent[n1]; } visited[n1] = true ; // For second node finding the first // node common while (!visited[n2]) n2 = parent[n2]; return n2; } // Insert function for Binary tree static void insertAdj( int []parent, int i, int j) { parent[i] = j; } // Driver Code public static void Main(String[] args) { // Maximum capacity of binary tree int []parent = new int [MAX]; // Root marked parent[20] = -1; insertAdj(parent, 8, 20); insertAdj(parent, 22, 20); insertAdj(parent, 4, 8); insertAdj(parent, 12, 8); insertAdj(parent, 10, 12); insertAdj(parent, 14, 12); Console.WriteLine(findLCA(10, 14, parent)); } } // This code is contributed by 29AjayKumar |
12
Time Complexity – The time complexity of the above algorithm is O(log n) as it requires O(log n) time in searching.
Recommended Posts:
- Lowest Common Ancestor in a Binary Tree | Set 2 (Using Parent Pointer)
- Lowest Common Ancestor in a Binary Tree | Set 1
- Lowest Common Ancestor in a Binary Tree | Set 3 (Using RMQ)
- Lowest Common Ancestor in a Binary Search Tree.
- Construct Binary Tree from given Parent Array representation
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Tarjan's off-line lowest common ancestors algorithm
- Height of n-ary tree if parent array is given
- Height of a generic tree from parent array
- Find Height of Binary Tree represented by Parent array
- Maximum number of consecutive 1's in binary representation of all the array elements
- Shortest path between two nodes in array like representation of binary tree
- Minimum sub-array such that number of 1's in concatenation of binary representation of its elements is at least K
- Find K items with the lowest values
- Count pairs in an array which have at least one digit common
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.