Skip to content

Commit a2ddf9e

Browse files
solves #106 in java
1 parent 252c296 commit a2ddf9e

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Diff for: src/ConstructBinaryTreeFromInorderAndPostorderTraversal.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ public class ConstructBinaryTreeFromInorderAndPostorderTraversal {
1010
int[] postOrder;
1111

1212
public TreeNode buildTree(int[] inorder, int[] postorder) {
13-
inOrderIndices = getIndicesMap(inorder);
13+
this.inOrderIndices = getIndicesMap(inorder);
1414
this.postOrder = postorder;
1515
return buildTreePostIn(0, inorder.length - 1, 0, postorder.length - 1);
1616
}
1717

18-
private TreeNode buildTreePostIn(int is, int ie, int ps, int pe){
19-
if (ps > pe || is > ie) return null;
20-
final TreeNode root = new TreeNode(postOrder[pe]);
21-
int ri = inOrderIndices.get(postOrder[pe]);
22-
root.left = buildTreePostIn(is, ri-1, ps, ps+ri-is-1);
23-
root.right = buildTreePostIn(ri+1, ie, ps+ri-is, pe-1);
18+
private TreeNode buildTreePostIn(int inOrderStart, int inOrderEnd, int postOrderStart, int postOrderEnd) {
19+
if (postOrderStart > postOrderEnd || inOrderStart > inOrderEnd) return null;
20+
final TreeNode root = new TreeNode(postOrder[postOrderEnd]);
21+
int ri = inOrderIndices.get(postOrder[postOrderEnd]);
22+
root.left = buildTreePostIn(inOrderStart, ri - 1, postOrderStart, postOrderStart + ri - inOrderStart - 1);
23+
root.right = buildTreePostIn(ri + 1, inOrderEnd, postOrderStart + ri - inOrderStart, postOrderEnd - 1);
2424
return root;
2525
}
2626

0 commit comments

Comments
 (0)