forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0105-construct-binary-tree-from-preorder-and-inorder-traversal.java
82 lines (69 loc) · 2.17 KB
/
0105-construct-binary-tree-from-preorder-and-inorder-traversal.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 0 || inorder.length == 0) return null;
TreeNode root = new TreeNode(preorder[0]);
int mid = 0;
for (int i = 0; i < inorder.length; i++) {
if (preorder[0] == inorder[i]) mid = i;
}
root.left =
buildTree(
Arrays.copyOfRange(preorder, 1, mid + 1),
Arrays.copyOfRange(inorder, 0, mid)
);
root.right =
buildTree(
Arrays.copyOfRange(preorder, mid + 1, preorder.length),
Arrays.copyOfRange(inorder, mid + 1, inorder.length)
);
return root;
}
}
// Solution without using Array copies
class Solution {
Map<Integer, Integer> inorderPositions = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length < 1 || inorder.length < 1) return null;
for (int i = 0; i < inorder.length; i++) {
inorderPositions.put(inorder[i], i);
}
return builder(preorder, 0, 0, inorder.length - 1);
}
public TreeNode builder(
int[] preorder,
int preorderIndex,
int inorderLow,
int inorderHigh
) {
if (
preorderIndex > preorder.length - 1 || inorderLow > inorderHigh
) return null;
int currentVal = preorder[preorderIndex];
TreeNode n = new TreeNode(currentVal);
int mid = inorderPositions.get(currentVal);
n.left = builder(preorder, preorderIndex + 1, inorderLow, mid - 1);
n.right =
builder(
preorder,
preorderIndex + (mid - inorderLow) + 1,
mid + 1,
inorderHigh
);
return n;
}
}