-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_106v2.java
42 lines (35 loc) · 1.32 KB
/
LC_106v2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.HashMap;
public class LC_106v2 {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
static HashMap<Integer, Integer> map;
public TreeNode buildTree(int[] inorder, int[] postorder) {
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++)
map.put(inorder[i], i);
return build(postorder, 0, postorder.length - 1, inorder, 0, inorder.length - 1);
}
public static TreeNode build(int[] postorder, int postStart, int postEnd, int[] inorder, int inStart, int inEnd) {
if (postStart > postEnd|| inStart > inEnd) return null;
int rootVal = postorder[postEnd];
TreeNode root = new TreeNode(rootVal);
int rootInorderIdx = map.get(rootVal);
int leftSize = rootInorderIdx - inStart;
root.left = build(postorder, postStart, postStart + leftSize - 1, inorder, inStart, rootInorderIdx - 1);
root.right = build(postorder, postStart + leftSize, postEnd - 1, inorder, rootInorderIdx + 1, inEnd);
return root;
}
}