|
| 1 | +package com.fishercoder; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.TreeNode; |
| 4 | +import com.fishercoder.common.utils.TreeUtils; |
| 5 | +import com.fishercoder.solutions._235; |
| 6 | +import java.util.Arrays; |
| 7 | +import org.junit.BeforeClass; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import static org.junit.Assert.assertEquals; |
| 11 | + |
| 12 | +public class _235Test { |
| 13 | + private static _235.Solution1 solution1; |
| 14 | + private static _235.Solution2 solution2; |
| 15 | + private static TreeNode root; |
| 16 | + private static TreeNode p; |
| 17 | + private static TreeNode q; |
| 18 | + |
| 19 | + @BeforeClass |
| 20 | + public static void setup() { |
| 21 | + solution1 = new _235.Solution1(); |
| 22 | + solution2 = new _235.Solution2(); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void test1() { |
| 27 | + root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5)); |
| 28 | + TreeUtils.printBinaryTree(root); |
| 29 | + |
| 30 | + p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5)); |
| 31 | + TreeUtils.printBinaryTree(p); |
| 32 | + |
| 33 | + q = TreeUtils.constructBinaryTree(Arrays.asList(8, 7, 9)); |
| 34 | + TreeUtils.printBinaryTree(q); |
| 35 | + |
| 36 | + assertEquals(root, solution1.lowestCommonAncestor(root, p, q)); |
| 37 | + assertEquals(root, solution2.lowestCommonAncestor(root, p, q)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void test2() { |
| 42 | + root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5)); |
| 43 | + TreeUtils.printBinaryTree(root); |
| 44 | + |
| 45 | + p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5)); |
| 46 | + TreeUtils.printBinaryTree(p); |
| 47 | + |
| 48 | + q = TreeUtils.constructBinaryTree(Arrays.asList(4)); |
| 49 | + TreeUtils.printBinaryTree(q); |
| 50 | + |
| 51 | + assertEquals(p, solution1.lowestCommonAncestor(root, p, q)); |
| 52 | + assertEquals(p, solution2.lowestCommonAncestor(root, p, q)); |
| 53 | + } |
| 54 | +} |
0 commit comments