Skip to content

Commit a214676

Browse files
add a solution for 106
1 parent 8014ff0 commit a214676

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

+27
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,31 @@ private TreeNode buildTreeRecursively(Map<Integer, Integer> inorderMap, int inor
5656
return root;
5757
}
5858
}
59+
60+
public static class Solution2 {
61+
/**
62+
* My own solution after inspiration from LeetCode 105.
63+
* I go from the right to the left for the postorder array, also, I build the tree by forming the right subtree first and then the left subtree.
64+
* A bit different from using numsLeft from LeetCode 106, I use numsRight, meaning the number of nodes needed to form the right subtree in the inorder array.
65+
*/
66+
public TreeNode buildTree(int[] inorder, int[] postorder) {
67+
Map<Integer, Integer> inMap = new HashMap<>();
68+
for (int i = 0; i < inorder.length; i++) {
69+
inMap.put(inorder[i], i);
70+
}
71+
return helper(postorder, inorder, inMap, postorder.length - 1, 0, 0, inorder.length - 1);
72+
}
73+
74+
private TreeNode helper(int[] postorder, int[] inorder, Map<Integer, Integer> inMap, int postStart, int postEnd, int inStart, int inEnd) {
75+
if (postStart < postEnd) {
76+
return null;
77+
}
78+
int inRoot = inMap.get(postorder[postStart]);
79+
int numsRight = inEnd - inRoot;
80+
TreeNode node = new TreeNode(postorder[postStart]);
81+
node.right = helper(postorder, inorder, inMap, postStart - 1, postStart - numsRight, inRoot + 1, inEnd);
82+
node.left = helper(postorder, inorder, inMap, postStart - numsRight - 1, postEnd, inStart, inRoot - 1);
83+
return node;
84+
}
85+
}
5986
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
public class _106Test {
1414
private static _106.Solution1 solution1;
15+
private static _106.Solution2 solution2;
1516
private static TreeNode expected;
1617
private static TreeNode actual;
1718
private static int[] inorder;
@@ -20,6 +21,7 @@ public class _106Test {
2021
@BeforeClass
2122
public static void setup() {
2223
solution1 = new _106.Solution1();
24+
solution2 = new _106.Solution2();
2325
}
2426

2527
@Test
@@ -36,6 +38,8 @@ public void test1() {
3638
actual = solution1.buildTree(inorder, postorder);
3739
expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 2));
3840
assertEquals(expected, actual);
41+
actual = solution2.buildTree(inorder, postorder);
42+
assertEquals(expected, actual);
3943
}
4044

4145
@Test
@@ -52,7 +56,7 @@ public void test2() {
5256
* 4
5357
*/
5458
postorder = new int[]{4, 2, 5, 1, 3};
55-
inorder = new int[]{1, 2, 4, 5, 3};
59+
inorder = new int[]{1, 2, 4, 5, 3};
5660
actual = solution1.buildTree(inorder, postorder);
5761
expected = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4));
5862
assertEquals(expected, actual);

0 commit comments

Comments
 (0)