-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode106.java
More file actions
48 lines (41 loc) · 1.69 KB
/
LeetCode106.java
File metadata and controls
48 lines (41 loc) · 1.69 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
import util.TreeNode;
public class LeetCode106 {
public static void main(String[] args) {
// 输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
// 输出:[3,9,20,null,null,15,7]
System.out.println(new Solution106().buildTree(new int[] { 9, 3, 15, 20, 7 }, new int[] { 9, 15, 7, 20, 3 }));
// 输入:inorder = [-1], postorder = [-1]
// 输出:[-1]
System.out.println(new Solution106().buildTree(new int[] { -1 }, new int[] {
-1 }));
// 输入:inorder = [2,1], postorder = [2,1]
// 输出:
System.out.println(new Solution106().buildTree(new int[] { 2, 1 }, new int[] {
2, 1 }));
}
}
class Solution106 {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return dfs(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);
}
public TreeNode dfs(int[] inorder, int[] postorder, int in_start, int in_end, int post_start, int post_end) {
if (in_start > in_end) {
return null;
}
TreeNode root = new TreeNode(postorder[post_end], null, null);
if (in_start == in_end) {
return root;
}
// NOTE: 这里可以利用哈希表加速查询
int root_pos = in_start;
while (root_pos <= in_end) {
if (inorder[root_pos] == root.val) {
break;
}
root_pos++;
}
root.left = dfs(inorder, postorder, in_start, root_pos - 1, post_start, post_start + root_pos - 1 - in_start);
root.right = dfs(inorder, postorder, root_pos + 1, in_end, post_start + root_pos - in_start, post_end - 1);
return root;
}
}