|
3 | 3 | import com.fishercoder.common.classes.TreeNode;
|
4 | 4 | import com.fishercoder.common.utils.TreeUtils;
|
5 | 5 | import com.fishercoder.solutions._105;
|
| 6 | + |
6 | 7 | import java.util.Arrays;
|
| 8 | + |
7 | 9 | import org.junit.BeforeClass;
|
8 | 10 | import org.junit.Test;
|
9 | 11 |
|
10 | 12 | import static junit.framework.Assert.assertEquals;
|
11 | 13 |
|
12 | 14 | public class _105Test {
|
13 |
| - private static _105.Solution1 solution1; |
14 |
| - private static TreeNode expected; |
15 |
| - private static TreeNode actual; |
16 |
| - private static int[] preorder; |
17 |
| - private static int[] inorder; |
18 |
| - |
19 |
| - @BeforeClass |
20 |
| - public static void setup() { |
21 |
| - solution1 = new _105.Solution1(); |
22 |
| - } |
23 |
| - |
24 |
| - @Test |
25 |
| - public void test1() { |
26 |
| - preorder = new int[] {1, 2, 3}; |
27 |
| - inorder = new int[] {2, 1, 3}; |
28 |
| - actual = solution1.buildTree(preorder, inorder); |
29 |
| - expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); |
30 |
| - assertEquals(expected, actual); |
31 |
| - } |
32 |
| - |
33 |
| - @Test |
34 |
| - public void test2() { |
35 |
| - preorder = new int[] {1, 2, 4, 5, 3}; |
36 |
| - inorder = new int[] {4, 2, 5, 1, 3}; |
37 |
| - actual = solution1.buildTree(preorder, inorder); |
38 |
| - expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); |
39 |
| - assertEquals(expected, actual); |
40 |
| - } |
| 15 | + private static _105.Solution1 solution1; |
| 16 | + private static TreeNode expected; |
| 17 | + private static TreeNode actual; |
| 18 | + private static int[] preorder; |
| 19 | + private static int[] inorder; |
| 20 | + |
| 21 | + @BeforeClass |
| 22 | + public static void setup() { |
| 23 | + solution1 = new _105.Solution1(); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void test1() { |
| 28 | + preorder = new int[]{1, 2, 3}; |
| 29 | + inorder = new int[]{2, 1, 3}; |
| 30 | + actual = solution1.buildTree(preorder, inorder); |
| 31 | + expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3)); |
| 32 | + assertEquals(expected, actual); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void test2() { |
| 37 | + preorder = new int[]{1, 2, 4, 5, 3}; |
| 38 | + inorder = new int[]{4, 2, 5, 1, 3}; |
| 39 | + actual = solution1.buildTree(preorder, inorder); |
| 40 | + expected = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5)); |
| 41 | + assertEquals(expected, actual); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void test3() { |
| 46 | + preorder = new int[]{3, 9, 20, 15, 7}; |
| 47 | + inorder = new int[]{9, 3, 15, 20, 7}; |
| 48 | + actual = solution1.buildTree(preorder, inorder); |
| 49 | + expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7)); |
| 50 | + assertEquals(expected, actual); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void test4() { |
| 55 | + preorder = new int[]{3, 1, 2, 4}; |
| 56 | + inorder = new int[]{1, 2, 3, 4}; |
| 57 | + actual = solution1.buildTree(preorder, inorder); |
| 58 | + expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, 4, null, 2)); |
| 59 | + TreeUtils.printBinaryTree(expected); |
| 60 | + assertEquals(expected, actual); |
| 61 | + } |
41 | 62 | }
|
0 commit comments