Skip to content

Commit b130833

Browse files
refactor 106
1 parent 5d95766 commit b130833

File tree

2 files changed

+52
-53
lines changed

2 files changed

+52
-53
lines changed

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

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,58 @@
1212
1313
Note:
1414
You may assume that duplicates do not exist in the tree.
15-
1615
*/
1716
public class _106 {
17+
public static class Solution1 {
1818

19-
/**
20-
* https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space
21-
*
22-
* Note: the last element of postorder array is the root!
23-
*
24-
* The idea is to take the last element in postorder as the root; find the position of the root in the inorder array;
25-
* then locate the range for left sub-tree and right sub-tree and do recursion,
26-
* use a hashmap to record the index of root in the inorder array.
27-
*/
28-
public TreeNode buildTree(int[] inorder, int[] postorder) {
29-
if (inorder == null || postorder == null || inorder.length != postorder.length) {
30-
return null;
31-
}
32-
HashMap<Integer, Integer> inorderMap = new HashMap<>();
33-
for (int i = 0; i < inorder.length; i++) {
34-
inorderMap.put(inorder[i], i);
19+
/**
20+
* https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space
21+
*
22+
* Note: the last element of postorder array is the root!
23+
*
24+
* The idea is to take the last element in postorder as the root; find the position of the root
25+
* in the inorder array; then locate the range for left sub-tree and right sub-tree and do
26+
* recursion, use a hashmap to record the index of root in the inorder array.
27+
*/
28+
public TreeNode buildTree(int[] inorder, int[] postorder) {
29+
if (inorder == null || postorder == null || inorder.length != postorder.length) {
30+
return null;
31+
}
32+
HashMap<Integer, Integer> inorderMap = new HashMap<>();
33+
for (int i = 0; i < inorder.length; i++) {
34+
inorderMap.put(inorder[i], i);
35+
}
36+
/**At the beginning, both start from 0 to nums.length-1*/
37+
return buildTreeRecursively(inorderMap, 0, inorder.length - 1, postorder, 0,
38+
postorder.length - 1);
3539
}
36-
/**At the beginning, both start from 0 to nums.length-1*/
37-
return buildTreeRecursively(inorderMap, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
38-
}
3940

40-
private TreeNode buildTreeRecursively(Map<Integer, Integer> inorderMap, int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) {
41-
if (postorderStart > postorderEnd || inorderStart > inorderEnd) {
42-
return null;
43-
}
44-
TreeNode root = new TreeNode(postorder[postorderEnd]);
45-
int inRoot = inorderMap.get(postorder[postorderEnd]);
46-
int numsLeft = inRoot - inorderStart;
41+
private TreeNode buildTreeRecursively(Map<Integer, Integer> inorderMap, int inorderStart,
42+
int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) {
43+
if (postorderStart > postorderEnd || inorderStart > inorderEnd) {
44+
return null;
45+
}
46+
TreeNode root = new TreeNode(postorder[postorderEnd]);
47+
int inRoot = inorderMap.get(postorder[postorderEnd]);
48+
int numsLeft = inRoot - inorderStart;
4749

48-
/**It's easy to understand and remember:
49-
* for the indices of inorder array:
50-
* inStart and inRoot-1 as new start and end indices
51-
* inRoot+1 and inEnd as new start and end indices
52-
*
53-
* this is easy to understand and remember: since inRoot is already been used in this recursion call, so we're going to use inRoot-1 and inRoot+1 for next recursion call
54-
*
55-
* for the indices of postorder array:
56-
* postorderStart and postorderStart+numsLeft-1 should be the new start and end indices
57-
* postorderStart+numsLeft and postorderEnd-1 should be the new start and end indices
58-
*
59-
* this is also easy to understand and remember:
60-
* since the last one in postorder is the root and we have used it in this recursion call already, so the end is definitely postorderEnd-1;
61-
* then the postorderEnd for root.left is contiguous to the postorderStart of root.right, :)*/
62-
root.left = buildTreeRecursively(inorderMap, inorderStart, inRoot - 1, postorder, postorderStart, postorderStart + numsLeft - 1);
63-
root.right = buildTreeRecursively(inorderMap, inRoot + 1, inorderEnd, postorder, postorderStart + numsLeft, postorderEnd - 1);
64-
return root;
50+
/**It's easy to understand and remember:
51+
* for the indices of inorder array:
52+
* inStart and inRoot-1 as new start and end indices
53+
* inRoot+1 and inEnd as new start and end indices
54+
*
55+
* this is easy to understand and remember: since inRoot is already been used in this recursion call, so we're going to use inRoot-1 and inRoot+1 for next recursion call
56+
*
57+
* for the indices of postorder array:
58+
* postorderStart and postorderStart+numsLeft-1 should be the new start and end indices
59+
* postorderStart+numsLeft and postorderEnd-1 should be the new start and end indices
60+
*
61+
* this is also easy to understand and remember:
62+
* since the last one in postorder is the root and we have used it in this recursion call already, so the end is definitely postorderEnd-1;
63+
* then the postorderEnd for root.left is contiguous to the postorderStart of root.right, :)*/
64+
root.left = buildTreeRecursively(inorderMap, inorderStart, inRoot - 1, postorder, postorderStart, postorderStart + numsLeft - 1);
65+
root.right = buildTreeRecursively(inorderMap, inRoot + 1, inorderEnd, postorder, postorderStart + numsLeft, postorderEnd - 1);
66+
return root;
67+
}
6568
}
66-
6769
}

src/test/java/com/fishercoder/_106Test.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@
1010

1111
import static junit.framework.Assert.assertEquals;
1212

13-
/**
14-
* Created by fishercoder on 5/12/17.
15-
*/
1613
public class _106Test {
17-
private static _106 test;
14+
private static _106.Solution1 solution1;
1815
private static TreeNode expected;
1916
private static TreeNode actual;
2017
private static int[] inorder;
2118
private static int[] postorder;
2219

2320
@BeforeClass
2421
public static void setup() {
25-
test = new _106();
22+
solution1 = new _106.Solution1();
2623
}
2724

2825
@Test
@@ -36,7 +33,7 @@ public void test1() {
3633
*/
3734
postorder = new int[]{2, 1, 3};
3835
inorder = new int[]{1, 2, 3};
39-
actual = test.buildTree(inorder, postorder);
36+
actual = solution1.buildTree(inorder, postorder);
4037
expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 2));
4138
assertEquals(expected, actual);
4239
}
@@ -56,7 +53,7 @@ public void test2() {
5653
*/
5754
postorder = new int[]{4, 2, 5, 1, 3};
5855
inorder = new int[]{1, 2, 4, 5, 3};
59-
actual = test.buildTree(inorder, postorder);
56+
actual = solution1.buildTree(inorder, postorder);
6057
expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4));
6158
assertEquals(expected, actual);
6259
}
@@ -70,7 +67,7 @@ public void test3() {
7067
*/
7168
inorder = new int[]{1, 2};
7269
postorder = new int[]{1, 2};
73-
actual = test.buildTree(inorder, postorder);
70+
actual = solution1.buildTree(inorder, postorder);
7471
expected = TreeUtils.constructBinaryTree(Arrays.asList(2, 1));
7572
assertEquals(expected, actual);
7673
}

0 commit comments

Comments
 (0)