0% found this document useful (0 votes)
10 views2 pages

4

The document contains a Java program that constructs a tree from an array representation and provides methods to find the lowest common ancestor (LCA) and the distance between two nodes in the tree. The program defines a Node class to represent tree nodes and includes methods for displaying the tree, constructing it from an array, and finding paths and distances. The main method reads input, constructs the tree, and outputs the distance between two specified nodes.

Uploaded by

rakshit2000.rt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

4

The document contains a Java program that constructs a tree from an array representation and provides methods to find the lowest common ancestor (LCA) and the distance between two nodes in the tree. The program defines a Node class to represent tree nodes and includes methods for displaying the tree, constructing it from an array, and finding paths and distances. The main method reads input, constructs the tree, and outputs the distance between two specified nodes.

Uploaded by

rakshit2000.rt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;

public class Main {


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

public static void display(Node node) {


String str = [Link] + " -> ";
for (Node child : [Link]) {
str += [Link] + ", ";
}
str += ".";
[Link](str);

for (Node child : [Link]) {


display(child);
}
}

public static Node construct(int[] arr) {


Node root = null;

Stack<Node> st = new Stack<>();


for (int i = 0; i < [Link]; i++) {
if (arr[i] == -1) {
[Link]();
} else {
Node t = new Node();
[Link] = arr[i];

if ([Link]() > 0) {
[Link]().[Link](t);
} else {
root = t;
}

[Link](t);
}
}

return root;
}

public static ArrayList<Integer> nodeToRootPath(Node node, int data) {


if ([Link] == data) {
ArrayList<Integer> path = new ArrayList<>();
[Link]([Link]);
return path;
}

for (Node child : [Link]) {


ArrayList<Integer> ptc = nodeToRootPath(child, data);
if ([Link]() > 0) {
[Link]([Link]);
return ptc;
}
}

return new ArrayList<>();


}

public static int lca(Node node, int d1, int d2) {


ArrayList<Integer> p1 = nodeToRootPath(node, d1);
ArrayList<Integer> p2 = nodeToRootPath(node, d2);

int i = [Link]() - 1;
int j = [Link]() - 1;

while(i >= 0 && j >= 0 && [Link](i) == [Link](j)){


i--;
j--;
}

return [Link](i + 1);


}

public static int distanceBetweenNodes(Node node, int d1, int d2){


ArrayList<Integer> p1 = nodeToRootPath(node, d1);
ArrayList<Integer> p2 = nodeToRootPath(node, d2);

int i = [Link]() - 1;
int j = [Link]() - 1;

while(i >= 0 && j >= 0 && [Link](i) == [Link](j)){


i--;
j--;
}

i++;
j++;

return i + j;
}

public static void main(String[] args) throws Exception {


BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
int n = [Link]([Link]());
int[] arr = new int[n];
String[] values = [Link]().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = [Link](values[i]);
}

int d1 = [Link]([Link]());
int d2 = [Link]([Link]());

Node root = construct(arr);


int dist = distanceBetweenNodes(root, d1, d2);
[Link](dist);
// display(root);
}

You might also like