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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

Output:

12

Time Complexity – The time complexity of the above algorithm is O(log n) as it requires O(log n) time in searching.



My Personal Notes arrow_drop_up

Coder only XD Do send reference if contacting me

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.



Improved By : SURENDRA_GANGWAR



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.