-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerializeAndDeserializeBST.java
More file actions
89 lines (72 loc) · 3.02 KB
/
Copy pathSerializeAndDeserializeBST.java
File metadata and controls
89 lines (72 loc) · 3.02 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
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
83
84
85
86
87
88
89
// Serialize and Deserialize BST
// See: https://leetcode.com/problems/serialize-and-deserialize-bst/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.*;
import leetcode.util.tree.TreeNode;
public class SerializeAndDeserializeBST {
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) return "";
String left = serialize(root.left);
String right = serialize(root.right);
if (!left.isEmpty()) left = "," + left;
if (!right.isEmpty()) right = "," + right;
return root.val + left + right;
}
// Decodes your encoded data to tree.
// Recursive solution.
public TreeNode deserialize(String data) {
if (data.isEmpty()) return null;
String[] vals = data.split(",");
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
for (int i = 1; i < vals.length; i++)
insert(root, Integer.parseInt(vals[i]));
return root;
}
private TreeNode insert(TreeNode root, int val) {
if (root == null) return new TreeNode(val);
if (val < root.val)
root.left = insert(root.left, val);
else
root.right = insert(root.right, val);
return root;
}
// Decodes your encoded data to tree.
// Iterative solution.
public TreeNode deserialize_va1(String data) {
if (data.isEmpty()) return null;
String[] vals = data.split(",");
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
for (int i = 1; i < vals.length; i++) {
int val = Integer.parseInt(vals[i]);
TreeNode curr = root;
while (curr != null) {
if (val < curr.val && curr.left != null) {
curr = curr.left;
} else if (val < curr.val && curr.left == null) {
curr.left = new TreeNode(val);
break;
} else if(curr.right != null) {
curr = curr.right;
} else {
curr.right = new TreeNode(val);
break;
}
}
}
return root;
}
}
public static void main(String[] args) {
Codec codec = new SerializeAndDeserializeBST().new Codec();
TreeNode root1 = initTree(5, 3, 8, 1, 4, 7, 9, null, null, null, null, null, null, null, 11);
TreeNode root2 = initTree();
System.out.println(codec.serialize(root1));
System.out.println(codec.serialize(root2));
TreeNode deserTree1 = codec.deserialize(codec.serialize(root1));
System.out.println(deserTree1);
TreeNode deserTree2 = codec.deserialize(codec.serialize(root2));
System.out.println(deserTree2);
}
}