|
3 | 3 | importcom.fishercoder.common.classes.TreeNode;
|
4 | 4 | importcom.fishercoder.common.utils.TreeUtils;
|
5 | 5 | importcom.fishercoder.solutions._105;
|
| 6 | + |
6 | 7 | importjava.util.Arrays;
|
| 8 | + |
7 | 9 | importorg.junit.BeforeClass;
|
8 | 10 | importorg.junit.Test;
|
9 | 11 |
|
10 | 12 | importstaticjunit.framework.Assert.assertEquals;
|
11 | 13 |
|
12 | 14 | publicclass_105Test {
|
13 |
| -privatestatic_105.Solution1solution1; |
14 |
| -privatestaticTreeNodeexpected; |
15 |
| -privatestaticTreeNodeactual; |
16 |
| -privatestaticint[]preorder; |
17 |
| -privatestaticint[]inorder; |
18 |
| - |
19 |
| -@BeforeClass |
20 |
| -publicstaticvoidsetup() { |
21 |
| -solution1 =new_105.Solution1(); |
22 |
| - } |
23 |
| - |
24 |
| -@Test |
25 |
| -publicvoidtest1() { |
26 |
| -preorder =newint[] {1,2,3}; |
27 |
| -inorder =newint[] {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 |
| -publicvoidtest2() { |
35 |
| -preorder =newint[] {1,2,4,5,3}; |
36 |
| -inorder =newint[] {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 | +privatestatic_105.Solution1solution1; |
| 16 | +privatestaticTreeNodeexpected; |
| 17 | +privatestaticTreeNodeactual; |
| 18 | +privatestaticint[]preorder; |
| 19 | +privatestaticint[]inorder; |
| 20 | + |
| 21 | +@BeforeClass |
| 22 | +publicstaticvoidsetup() { |
| 23 | +solution1 =new_105.Solution1(); |
| 24 | + } |
| 25 | + |
| 26 | +@Test |
| 27 | +publicvoidtest1() { |
| 28 | +preorder =newint[]{1,2,3}; |
| 29 | +inorder =newint[]{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 | +publicvoidtest2() { |
| 37 | +preorder =newint[]{1,2,4,5,3}; |
| 38 | +inorder =newint[]{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 | +publicvoidtest3() { |
| 46 | +preorder =newint[]{3,9,20,15,7}; |
| 47 | +inorder =newint[]{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 | +publicvoidtest4() { |
| 55 | +preorder =newint[]{3,1,2,4}; |
| 56 | +inorder =newint[]{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 | }
|