297. Serialize And Deserialize Binary Tree¶
Difficulty: Hard
LeetCode Problem View on GitHub
297. Serialize and Deserialize Binary Tree
Hard
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Example 1:

Input: root = [1,2,3,null,null,4,5] Output: [1,2,3,null,null,4,5]
Example 2:
Input: root = [] Output: []
Constraints:
- The number of nodes in the tree is in the range
[0, 104]. -1000 <= Node.val <= 1000
Solution¶
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) return "";
String res = "";
res += root.val;
res += ":";
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (q.size () > 0) {
int len = q.size();
for (int i = 0; i < len; i++) {
if (q.peek().left != null) {
q.offer(q.peek().left);
res += q.peek().left.val;
res += ":";
}
else {
res += "-100000";
res += ":";
}
if (q.peek().right != null) {
q.offer(q.peek().right);
res += q.peek().right.val;
res += ":";
}
else {
res += "-100000";
res += ":";
}
q.poll();
}
}
return res;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
ArrayList<Integer> res = new ArrayList<>();
String current = "";
for (int i = 0; i < data.length(); i++) {
if (data.charAt(i) == ':') {
int x = Integer.parseInt(current);
current = "";
res.add(x);
}
else current += data.charAt(i);
}
if (res.size() == 0) return null;
TreeNode root = new TreeNode(res.get(0));
int current_ind = 1;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (q.size() > 0) {
TreeNode current_node = q.poll();
int left = -100000, right = -100000;
if (current_ind < res.size()) left = res.get(current_ind++);
if (current_ind < res.size()) right = res.get(current_ind++);
if (left == -100000) current_node.left = null;
else {
current_node.left = new TreeNode(left);
q.offer(current_node.left);
}
if (right == -100000) current_node.right = null;
else {
current_node.right = new TreeNode(right);
q.offer(current_node.right);
}
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here