-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_106.java
More file actions
54 lines (46 loc) · 1.79 KB
/
Copy pathP_106.java
File metadata and controls
54 lines (46 loc) · 1.79 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
package leetcode.medium;
import java.util.HashMap;
import java.util.Map;
import utils.DataStructures.TreeNode;
@SuppressWarnings({ "ConstantConditions", "ReturnOfNull" })
public class P_106 {
public TreeNode buildTreeOld(int[] inorder, int[] postorder) {
final Map<Integer, Integer> inMap = new HashMap<>();
for (int i = 0; i < postorder.length; i++) {
inMap.put(inorder[i], i);
}
return helper(postorder, 0, inorder.length - 1, 0, postorder.length - 1, inMap);
}
private static TreeNode helper(int[] postorder, int inStart, int inEnd, int postStart, int postEnd,
Map<Integer, Integer> inMap) {
if (postStart > postEnd) {
return null;
}
final TreeNode root = new TreeNode(postorder[postEnd]);
final int i = inMap.get(root.val);
root.left = helper(postorder, inStart, i - 1, postStart, postStart + i - inStart - 1, inMap);
root.right = helper(postorder, i + 1, inEnd, postStart + i - inStart, postEnd - 1, inMap);
return root;
}
static int idx;
static Map<Integer, Integer> inMap;
public TreeNode buildTree(int[] inorder, int[] postorder) {
final int n = inorder.length;
inMap = new HashMap<>();
idx = n - 1;
for (int i = 0; i < n; i++) {
inMap.put(inorder[i], i);
}
return dfs(postorder, 0, n - 1);
}
private static TreeNode dfs(int[] postorder, int start, int end) {
if (start > end) {
return null;
}
final TreeNode root = new TreeNode(postorder[idx--]);
final int newEnd = inMap.get(root.val);
root.right = dfs(postorder, newEnd + 1, end);
root.left = dfs(postorder, start, newEnd - 1);
return root;
}
}