@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullElementsIterative() { lca.getIterative(null, null, null); }
public BinaryNode getIterative(BinaryNode root, BinaryNode n1, BinaryNode n2) { validateInput(root, n1, n2); List<BinaryNode> pathToA = getPathTo(root, n1); List<BinaryNode> pathToB = getPathTo(root, n2); BinaryNode result = null; int size = Math.min(pathToA.size(), pathToB.size()); for (int i = 0; i < size; i++) { if (pathToA.get(i) != pathToB.get(i)) { result = pathToA.get(i - 1); break; } } return result; }
LowestCommonAncestor { public BinaryNode getIterative(BinaryNode root, BinaryNode n1, BinaryNode n2) { validateInput(root, n1, n2); List<BinaryNode> pathToA = getPathTo(root, n1); List<BinaryNode> pathToB = getPathTo(root, n2); BinaryNode result = null; int size = Math.min(pathToA.size(), pathToB.size()); for (int i = 0; i < size; i++) { if (pathToA.get(i) != pathToB.get(i)) { result = pathToA.get(i - 1); break; } } return result; } }
LowestCommonAncestor { public BinaryNode getIterative(BinaryNode root, BinaryNode n1, BinaryNode n2) { validateInput(root, n1, n2); List<BinaryNode> pathToA = getPathTo(root, n1); List<BinaryNode> pathToB = getPathTo(root, n2); BinaryNode result = null; int size = Math.min(pathToA.size(), pathToB.size()); for (int i = 0; i < size; i++) { if (pathToA.get(i) != pathToB.get(i)) { result = pathToA.get(i - 1); break; } } return result; }  }
LowestCommonAncestor { public BinaryNode getIterative(BinaryNode root, BinaryNode n1, BinaryNode n2) { validateInput(root, n1, n2); List<BinaryNode> pathToA = getPathTo(root, n1); List<BinaryNode> pathToB = getPathTo(root, n2); BinaryNode result = null; int size = Math.min(pathToA.size(), pathToB.size()); for (int i = 0; i < size; i++) { if (pathToA.get(i) != pathToB.get(i)) { result = pathToA.get(i - 1); break; } } return result; }  BinaryNode getRecursive(BinaryNode root, BinaryNode n1, BinaryNode n2); BinaryNode getIterative(BinaryNode root, BinaryNode n1, BinaryNode n2); }
LowestCommonAncestor { public BinaryNode getIterative(BinaryNode root, BinaryNode n1, BinaryNode n2) { validateInput(root, n1, n2); List<BinaryNode> pathToA = getPathTo(root, n1); List<BinaryNode> pathToB = getPathTo(root, n2); BinaryNode result = null; int size = Math.min(pathToA.size(), pathToB.size()); for (int i = 0; i < size; i++) { if (pathToA.get(i) != pathToB.get(i)) { result = pathToA.get(i - 1); break; } } return result; }  BinaryNode getRecursive(BinaryNode root, BinaryNode n1, BinaryNode n2); BinaryNode getIterative(BinaryNode root, BinaryNode n1, BinaryNode n2);  }