public String LCA(String s, String t) throws IllegalArgumentException {
// Check if s or t are null or not in the tree
if (s == null || t == null || !stringsToNodes.containsKey(s) || !stringsToNodes.containsKey(t)) {
throw new IllegalArgumentException("s or t are null or not in the tree");
}
// Find nodes containing s and t
Node nodeS = stringsToNodes.get(s);
Node nodeT = stringsToNodes.get(t);
// Get paths from nodes to the root
ArrayStack pathS = getPathToRoot(nodeS);
ArrayStack pathT = getPathToRoot(nodeT);
// Find the intersection of paths
Node lca = findIntersection(pathS, pathT);
// Return the value of the LCA or null if not found
return lca != null ? lca.s : null;
}
// method to find the path from a node to the root
private ArrayStack getPathToRoot(Node node) {
ArrayStack path = new ArrayStack();
while (node != null) {
path.add(node);
node = node.parent;
}
return path;
}
// method to find the intersection of two paths
private Node findIntersection(ArrayStack pathS, ArrayStack pathT) {
Node intersect = null;
// Iterate over both paths until one of them is exhausted
while (!pathS.isEmpty() && !pathT.isEmpty()) {
Node nodeS = pathS.get(pathS.size() - 1);
Node nodeT = pathT.get(pathT.size() - 1);
if (nodeS == nodeT) {
intersect = nodeS;
} else {
break;
}
pathS.remove(pathS.size() - 1); // Move to the previous node in pathS
pathT.remove(pathT.size() - 1); // Move to the previous node in pathT
}
return intersect;
}
This is my function
I've tried doing it without the private methods, and I wasn't able to find any other solutions online.
[code]public String LCA(String s, String t) throws IllegalArgumentException { // Check if s or t are null or not in the tree if (s == null || t == null || !stringsToNodes.containsKey(s) || !stringsToNodes.containsKey(t)) { throw new IllegalArgumentException("s or t are null or not in the tree"); }
// Find nodes containing s and t Node nodeS = stringsToNodes.get(s); Node nodeT = stringsToNodes.get(t);
// Get paths from nodes to the root ArrayStack pathS = getPathToRoot(nodeS); ArrayStack pathT = getPathToRoot(nodeT);
// Find the intersection of paths Node lca = findIntersection(pathS, pathT);
// Return the value of the LCA or null if not found return lca != null ? lca.s : null; }
// method to find the path from a node to the root private ArrayStack getPathToRoot(Node node) { ArrayStack path = new ArrayStack(); while (node != null) { path.add(node); node = node.parent; } return path; }
// method to find the intersection of two paths private Node findIntersection(ArrayStack pathS, ArrayStack pathT) { Node intersect = null;
// Iterate over both paths until one of them is exhausted while (!pathS.isEmpty() && !pathT.isEmpty()) { Node nodeS = pathS.get(pathS.size() - 1); Node nodeT = pathT.get(pathT.size() - 1); if (nodeS == nodeT) { intersect = nodeS; } else { break; } pathS.remove(pathS.size() - 1); // Move to the previous node in pathS pathT.remove(pathT.size() - 1); // Move to the previous node in pathT }
return intersect; } [/code] This is my function I've tried doing it without the private methods, and I wasn't able to find any other solutions online.