Skip to content

Commit 8014ff0

Browse files
refactor 105
1 parent 0cc223a commit 8014ff0

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

src/main/java/com/fishercoder/solutions/_105.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public static class Solution1 {
1212
* credit: https://discuss.leetcode.com/topic/29838/5ms-java-clean-solution-with-caching use
1313
* HashMap as the cache so that accessing inorder index becomes O(1) time Note: The first
1414
* element of preorder array is the root!
15+
*
16+
* Using a pen and paper to visualize this helps a great deal!
17+
* preorder array has the root at the head, then we could use this number as a pivot in the inorder array: all elements on the left of this pivot should form a left subtree of this pivot
18+
* and anything on the right side of this pivot in the inorder array should form a right subtree of this pivot.
1519
*/
1620
public TreeNode buildTree(int[] preorder, int[] inorder) {
1721
Map<Integer, Integer> inorderMap = new HashMap();
@@ -23,14 +27,14 @@ public TreeNode buildTree(int[] preorder, int[] inorder) {
2327
return buildTree(preorder, 0, preorder.length - 1, inorderMap, 0, inorder.length - 1);
2428
}
2529

26-
private TreeNode buildTree(int[] preorder, int preStart, int preEnd,
27-
Map<Integer, Integer> inorderMap, int inStart, int inEnd) {
30+
private TreeNode buildTree(int[] preorder, int preStart, int preEnd, Map<Integer, Integer> inorderMap, int inStart, int inEnd) {
2831
if (preStart > preEnd || inStart > inEnd) {
2932
return null;
3033
}
3134

3235
TreeNode root = new TreeNode(preorder[preStart]);
3336
int inRoot = inorderMap.get(preorder[preStart]);
37+
//This line is the key to figure out how many nodes should be on the left subtree
3438
int numsLeft = inRoot - inStart;
3539

3640
/**It's easy to understand and remember:

src/test/java/com/fishercoder/_105Test.java

+49-28
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,60 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions._105;
6+
67
import java.util.Arrays;
8+
79
import org.junit.BeforeClass;
810
import org.junit.Test;
911

1012
import static junit.framework.Assert.assertEquals;
1113

1214
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+
}
4162
}

0 commit comments

Comments
 (0)