5
5
import java .util .HashMap ;
6
6
import java .util .Map ;
7
7
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
- */
16
8
public class _106 {
17
9
public static class Solution1 {
18
10
19
11
/**
20
12
* https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space
21
- *
22
13
* Note: the last element of postorder array is the root!
23
- *
24
14
* The idea is to take the last element in postorder as the root; find the position of the root
25
15
* in the inorder array; then locate the range for left sub-tree and right sub-tree and do
26
16
* 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) {
35
25
}
36
26
/**At the beginning, both start from 0 to nums.length-1*/
37
27
return buildTreeRecursively (inorderMap , 0 , inorder .length - 1 , postorder , 0 ,
38
- postorder .length - 1 );
28
+ postorder .length - 1 );
39
29
}
40
30
41
31
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 ) {
43
33
if (postorderStart > postorderEnd || inorderStart > inorderEnd ) {
44
34
return null ;
45
35
}
0 commit comments