0297. Serialize and Deserialize Binary Tree
https://leetcode.com/problems/serialize-and-deserialize-binary-tree
Description
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:** []
Example 3:
**Input:** root = [1]
**Output:** [1]
Example 4:
**Input:** root = [1,2]
**Output:** [1,2]
Constraints:
The number of nodes in the tree is in the range
[0, 104]
.-1000 <= Node.val <= 1000
ac1: BFS
/**
* 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) {
// edge cases
if (root == null) return "null";
StringBuilder sb = new StringBuilder();
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
TreeNode h = q.poll();
if (h == null) {
sb.append("null,");
} else {
sb.append("" + h.val + ",");
q.offer(h.left);
q.offer(h.right);
}
}
sb.setLength(sb.length()-1); // remove last ","
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] s = data.split(",");
if ("null".equals(s[0])) return null;
TreeNode root = new TreeNode(Integer.parseInt(s[0])); // set root
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int i = 1;
while(!q.isEmpty()) {
TreeNode h = q.poll();
String l = s[i++];
h.left = "null".equals(l) ? null : new TreeNode(Integer.parseInt(l));
String r = s[i++];
h.right = "null".equals(r) ? null : new TreeNode(Integer.parseInt(r));
if (h.left != null) q.offer(h.left);
if (h.right != null) q.offer(h.right);
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
/*
1. BFS
2. DFS, pre-order
*/
ac2: DFS pre-order
/**
* 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) {
StringBuilder sb = new StringBuilder();
seri(root, sb);
return sb.toString();
}
private void seri(TreeNode node, StringBuilder sb) {
// exit
if (node == null) {
sb.append("null,");
return;
}
sb.append("" + node.val + ",");
seri(node.left, sb);
seri(node.right, sb);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
Queue<String> q = new LinkedList<>();
for (String s : data.split(",")) {
q.offer(s);
}
return dese(q);
}
private TreeNode dese(Queue<String> q) {
String str = q.poll();
TreeNode root = "null".equals(str) ? null : new TreeNode(Integer.parseInt(str));
if (root != null) {
root.left = dese(q);
root.right = dese(q);
}
return root;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
/*
1. BFS
2. DFS, pre-order
*/
Last updated
Was this helpful?