-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerializeAndDeserializeBST.java
51 lines (45 loc) · 1.59 KB
/
SerializeAndDeserializeBST.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
package com.smlnskgmail.jaman.leetcodejava.medium;
import com.smlnskgmail.jaman.leetcodejava.support.TreeNode;
// https://leetcode.com/problems/serialize-and-deserialize-bst/
public class SerializeAndDeserializeBST {
public String serialize(TreeNode root) {
if (root == null) {
return "#";
}
StringBuilder result = new StringBuilder();
result.append(root.val);
String left = serialize(root.left);
result.append(",").append(left);
String right = serialize(root.right);
result.append(",").append(right);
return result.toString();
}
public TreeNode deserialize(String data) {
String[] parsedData = data.split(",");
return deserializeFromString(parsedData, 0, parsedData.length - 1);
}
private TreeNode deserializeFromString(String[] data, int l, int r) {
String candidate = data[l];
if (candidate.equals("#")) {
return null;
}
TreeNode root = new TreeNode(Integer.parseInt(data[l]));
boolean built = false;
for (int i = l + 1; i <= r; i++) {
String value = data[i];
if (value.equals("#")) {
continue;
}
if (Integer.parseInt(value) > root.val) {
built = true;
root.left = deserializeFromString(data, l + 1, i - 1);
root.right = deserializeFromString(data, i, r);
break;
}
}
if (!built) {
root.left = deserializeFromString(data, l + 1, r);
}
return root;
}
}