@Test public void testLowestCommonAncestor() { BinaryTreeNode<Integer> lca = lowestCommonAncestor(root, three, five); Assert.assertEquals(4, lca.data().intValue()); lca = lowestCommonAncestor(root, one, three); Assert.assertEquals(2, lca.data().intValue()); lca = lowestCommonAncestor(root, three, eight); Assert.assertEquals(6, lca.data().intValue()); }
public static BinaryTreeNode<Integer> lowestCommonAncestor(BinaryTreeNode<Integer> root, BinaryTreeNode<Integer> n1, BinaryTreeNode<Integer> n2) { if (root == null || root == n1 || root == n2) { return root; } BinaryTreeNode<Integer> left = lowestCommonAncestor(root.leftChild(), n1, n2); BinaryTreeNode<Integer> right = lowestCommonAncestor(root.rightChild(), n1, n2); if (left != null && right != null) { return root; } return left != null ? left : right; }
PracticeQuestionsCh9 { public static BinaryTreeNode<Integer> lowestCommonAncestor(BinaryTreeNode<Integer> root, BinaryTreeNode<Integer> n1, BinaryTreeNode<Integer> n2) { if (root == null || root == n1 || root == n2) { return root; } BinaryTreeNode<Integer> left = lowestCommonAncestor(root.leftChild(), n1, n2); BinaryTreeNode<Integer> right = lowestCommonAncestor(root.rightChild(), n1, n2); if (left != null && right != null) { return root; } return left != null ? left : right; } }
PracticeQuestionsCh9 { public static BinaryTreeNode<Integer> lowestCommonAncestor(BinaryTreeNode<Integer> root, BinaryTreeNode<Integer> n1, BinaryTreeNode<Integer> n2) { if (root == null || root == n1 || root == n2) { return root; } BinaryTreeNode<Integer> left = lowestCommonAncestor(root.leftChild(), n1, n2); BinaryTreeNode<Integer> right = lowestCommonAncestor(root.rightChild(), n1, n2); if (left != null && right != null) { return root; } return left != null ? left : right; }  }
PracticeQuestionsCh9 { public static BinaryTreeNode<Integer> lowestCommonAncestor(BinaryTreeNode<Integer> root, BinaryTreeNode<Integer> n1, BinaryTreeNode<Integer> n2) { if (root == null || root == n1 || root == n2) { return root; } BinaryTreeNode<Integer> left = lowestCommonAncestor(root.leftChild(), n1, n2); BinaryTreeNode<Integer> right = lowestCommonAncestor(root.rightChild(), n1, n2); if (left != null && right != null) { return root; } return left != null ? left : right; }  static BinaryTreeNode<Integer> lowestCommonAncestor(BinaryTreeNode<Integer> root,
			BinaryTreeNode<Integer> n1, BinaryTreeNode<Integer> n2); }
PracticeQuestionsCh9 { public static BinaryTreeNode<Integer> lowestCommonAncestor(BinaryTreeNode<Integer> root, BinaryTreeNode<Integer> n1, BinaryTreeNode<Integer> n2) { if (root == null || root == n1 || root == n2) { return root; } BinaryTreeNode<Integer> left = lowestCommonAncestor(root.leftChild(), n1, n2); BinaryTreeNode<Integer> right = lowestCommonAncestor(root.rightChild(), n1, n2); if (left != null && right != null) { return root; } return left != null ? left : right; }  static BinaryTreeNode<Integer> lowestCommonAncestor(BinaryTreeNode<Integer> root,
			BinaryTreeNode<Integer> n1, BinaryTreeNode<Integer> n2); static final Logger LOGGER; }