-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
35 lines (31 loc) · 1.22 KB
/
Copy pathsolution.java
File metadata and controls
35 lines (31 loc) · 1.22 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
/*
class Node {
int data;
Node left, right;
Node(int val) {
data = val;
left = right = null;
}
}
*/
import java.util.*;
class Solution {
public Node constructTree(int[] pre, int[] post) {
int n = pre.length;
if (n == 0) return null;
Map<Integer,Integer> pos = new HashMap<>();
for (int i = 0; i < n; ++i) pos.put(post[i], i);
return build(pre, post, 0, n - 1, 0, n - 1, pos);
}
private Node build(int[] pre, int[] post, int preL, int preR, int postL, int postR, Map<Integer,Integer> pos) {
if (preL > preR || postL > postR) return null;
Node root = new Node(pre[preL]); // root from preorder
if (preL == preR) return root; // leaf node
int leftRootVal = pre[preL + 1]; // root of left subtree
int idx = pos.get(leftRootVal); // index in postorder
int leftSize = idx - postL + 1; // number of nodes in left subtree
root.left = build(pre, post, preL + 1, preL + leftSize, postL, idx, pos);
root.right = build(pre, post, preL + leftSize + 1, preR, idx + 1, postR - 1, pos);
return root;
}
}