@Test void testLowestCommonAncestor() { IntegerNode five = IntegerNode.builder() .value(5) .build(); IntegerNode nine = IntegerNode.builder() .value(9) .build(); IntegerNode eleven = IntegerNode.builder() .left(nine) .right(five) .value(11) .build(); IntegerNode two = IntegerNode.builder() .value(2) .build(); IntegerNode six = IntegerNode.builder() .left(two) .right(eleven) .value(6) .build(); IntegerNode seven = IntegerNode.builder() .value(7) .build(); IntegerNode thirteen = IntegerNode.builder() .left(seven) .value(13) .build(); IntegerNode eight = IntegerNode.builder() .right(thirteen) .value(8) .build(); IntegerNode three = IntegerNode.builder() .left(six) .right(eight) .value(3) .build(); print(three); IntegerNode lca = lowestCommonAncestor(three, 2, 5); assertThat(lca).isNotNull(); assertThat(lca.value()).isEqualTo(6); lca = lowestCommonAncestor(three, 8, 11); assertThat(lca).isNotNull(); assertThat(lca.value()).isEqualTo(3); lca = lowestCommonAncestor(three, 2, 19); assertThat(lca).isNull(); }
public static IntegerNode lowestCommonAncestor(IntegerNode root, int value1, int value2) { Map.Entry<IntegerNode, Boolean> lca = internalLCA(root, value1, value2); return lca != null && lca.getValue() ? lca.getKey() : null; }
BinaryTreeUtil { public static IntegerNode lowestCommonAncestor(IntegerNode root, int value1, int value2) { Map.Entry<IntegerNode, Boolean> lca = internalLCA(root, value1, value2); return lca != null && lca.getValue() ? lca.getKey() : null; } }
BinaryTreeUtil { public static IntegerNode lowestCommonAncestor(IntegerNode root, int value1, int value2) { Map.Entry<IntegerNode, Boolean> lca = internalLCA(root, value1, value2); return lca != null && lca.getValue() ? lca.getKey() : null; } private  BinaryTreeUtil(); }
BinaryTreeUtil { public static IntegerNode lowestCommonAncestor(IntegerNode root, int value1, int value2) { Map.Entry<IntegerNode, Boolean> lca = internalLCA(root, value1, value2); return lca != null && lca.getValue() ? lca.getKey() : null; } private  BinaryTreeUtil(); static int height(IntegerNode root); static boolean isBST(ParentAwareIntegerNode root); static boolean isSame(IntegerNode root1, IntegerNode root2); static IntegerNode lowestCommonAncestor(IntegerNode root, int value1, int value2); }
BinaryTreeUtil { public static IntegerNode lowestCommonAncestor(IntegerNode root, int value1, int value2) { Map.Entry<IntegerNode, Boolean> lca = internalLCA(root, value1, value2); return lca != null && lca.getValue() ? lca.getKey() : null; } private  BinaryTreeUtil(); static int height(IntegerNode root); static boolean isBST(ParentAwareIntegerNode root); static boolean isSame(IntegerNode root1, IntegerNode root2); static IntegerNode lowestCommonAncestor(IntegerNode root, int value1, int value2);  }