-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path106.java
More file actions
20 lines (18 loc) · 789 Bytes
/
106.java
File metadata and controls
20 lines (18 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int find(int l, int r, int need, int[] a) {
for (int i = l; i <= r; i++) if (a[i] == need) return i;
return -1;
}
public TreeNode help(int inb, int ind, int pb, int pd, int[] inorder, int[] postorder) {
if (inb > ind || pb > pd) return null;
TreeNode root = new TreeNode(postorder[pd]);
int index = find(inb, ind, postorder[pd], inorder);
root.left = help(inb, index - 1, pb, pd - (ind - index) - 1, inorder, postorder);
root.right = help(index + 1, ind, pd - (ind - index), pd - 1, inorder, postorder);
return root;
}
public TreeNode buildTree(int[] inorder, int[] postorder) {
int l = inorder.length;
return help(0, l - 1, 0, l - 1, inorder, postorder);
}
}