Skip to content

Commit 3185ec4

Browse files
refactor 106
1 parent c7c7e0f commit 3185ec4

File tree

1 file changed

+2
-12
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+2
-12
lines changed

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,12 @@
55
import java.util.HashMap;
66
import java.util.Map;
77

8-
/**
9-
* 106. Construct Binary Tree from Inorder and Postorder Traversal
10-
*
11-
* Given inorder and postorder traversal of a tree, construct the binary tree.
12-
13-
Note:
14-
You may assume that duplicates do not exist in the tree.
15-
*/
168
public class _106 {
179
public static class Solution1 {
1810

1911
/**
2012
* https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space
21-
*
2213
* Note: the last element of postorder array is the root!
23-
*
2414
* The idea is to take the last element in postorder as the root; find the position of the root
2515
* in the inorder array; then locate the range for left sub-tree and right sub-tree and do
2616
* recursion, use a hashmap to record the index of root in the inorder array.
@@ -35,11 +25,11 @@ public TreeNode buildTree(int[] inorder, int[] postorder) {
3525
}
3626
/**At the beginning, both start from 0 to nums.length-1*/
3727
return buildTreeRecursively(inorderMap, 0, inorder.length - 1, postorder, 0,
38-
postorder.length - 1);
28+
postorder.length - 1);
3929
}
4030

4131
private TreeNode buildTreeRecursively(Map<Integer, Integer> inorderMap, int inorderStart,
42-
int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) {
32+
int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) {
4333
if (postorderStart > postorderEnd || inorderStart > inorderEnd) {
4434
return null;
4535
}

0 commit comments

Comments
 (0)