Tuesday, March 1, 2022

Lowest Common Ancestor (generic Tree)

I/P:

24 10 20 50 -1 60 -1 -1 30 70 -1 80 110 -1 120 -1 -1 90 -1 -1 40 100 -1 -1 -1 120 80


O/P: 80



Store node-to-root path of the first node in array p1. 

Store node-to-root path of the second node in array p2. 

Initialize two pointers i and j as p1.length - 1 and p2.length - 1 respectively. 

Decrement i and j until arr[i] and arr[j] become unequal. 

The lowest common ancestor will be arr[i + 1] (or arr[j + 1]).




package pep.Day29;

import java.io.*;
import java.util.*;

public class Lowest_Common_Ancestor_GenericTree {

private static class Node {
int data;
ArrayList<Node> children = new ArrayList<>();
}

public static void display(Node node) {
String str = node.data + " -> ";
for (Node child : node.children) {
str += child.data + ", ";
}
str += ".";
System.out.println(str);

for (Node child : node.children) {
display(child);
}
}

public static Node construct(int[] arr) {
Node root = null;

Stack<Node> st = new Stack<>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == -1) {
st.pop();
} else {
Node t = new Node();
t.data = arr[i];

if (st.size() > 0) {
st.peek().children.add(t);
} else {
root = t;
}

st.push(t);
}
}

return root;
}

public static ArrayList<Integer> nodeToRootPath(Node node, int data) {
if (node.data == data) {
ArrayList<Integer> base = new ArrayList<>();
base.add(data);
return base;
}

for (Node child : node.children) {
ArrayList<Integer> pathTillChild = nodeToRootPath(child, data);
if (pathTillChild.size() > 0) {
pathTillChild.add(child.data);
return pathTillChild;
}
}

return new ArrayList<Integer>();
}

public static int lca(Node node, int d1, int d2) {
ArrayList<Integer> first = nodeToRootPath(node, d1);
ArrayList<Integer> second = nodeToRootPath(node, d2);

int i = first.size() - 1, j = second.size() - 1;
while (i >= 0 && j >= 0 && first.get(i) == second.get(j)) {
i--;
j--;
}
return first.get(i + 1);

}

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());

int[] arr = new int[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(values[i]);
}

int d1 = Integer.parseInt(br.readLine());
int d2 = Integer.parseInt(br.readLine());

Node root = construct(arr);
int lca = lca(root, d1, d2);
System.out.println(lca);
// display(root);
}

}


Time Complexity: O(n) Finding the node in the entire tree to get it's node to the root path is an O(n) task. Then, just traversing the node-to-root path (arrays) takes O(d) where d = depth of node. In the worst case, d can be equal to n, hence total time complexity will be O(n) only.

Space Complexity: We are storing node-to-root paths in arraylists. This will take O(n) auxiliary space.


No comments:

Post a Comment

Diagonal Traversal

 eg.  1       2       3       4 5      6       7       8 9    10    11     12 13  14   15    16 Output: 1 6 11 16 2 7 12 3 8 4  Approach:...