-
Notifications
You must be signed in to change notification settings - Fork 46
/
_449_1.java
67 lines (60 loc) · 2.36 KB
/
_449_1.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
import java.util.Arrays;
import java.util.StringJoiner;
/**
* LeetCode 449 - Serialize and Deserialize BST
* <p>
* An optimized version
* O(n) - Serialize
* O(n log n) - Deserialize
*/
public class _449_1 {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
final StringJoiner joiner = new StringJoiner(" ");
class Serializer {
public void dfs(TreeNode root) {
if (root == null) return;
joiner.add("" + root.val);
dfs(root.left);
dfs(root.right);
}
}
new Serializer().dfs(root);
return joiner.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
data = data.trim();
if (data.length() <= 0) return null;
String[] tokens = data.trim().split(" ");
int[] preOrder = new int[tokens.length];
for (int i = 0; i < preOrder.length; i++)
preOrder[i] = Integer.parseInt(tokens[i]);
int[] inOrder = preOrder.clone();
Arrays.sort(inOrder);
class Deserializer {
public TreeNode dfs(int preL, int preR, int inL, int inR) {
if (preR - preL != inR - inL)
throw new IllegalStateException("Inconsistent preorder and inorder sequences.");
if (preL > preR) return null;
TreeNode root = new TreeNode(preOrder[preL]);
int left = inL, right = inR;
while (left <= right) {
int mid = (left + right) / 2;
if (inOrder[mid] == root.val) {
int leftChildSize = mid - inL;
root.left = dfs(preL + 1, preL + leftChildSize, inL, mid - 1);
root.right = dfs(preL + leftChildSize + 1, preR, mid + 1, inR);
return root;
} else if (inOrder[mid] < root.val) left = mid + 1;
else right = mid - 1;
}
throw new IllegalStateException("Inconsistent preorder and inorder sequences.");
}
}
return new Deserializer().dfs(0, preOrder.length - 1, 0, inOrder.length - 1);
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));