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.
Example:
You may serialize the following tree:
1
/ \
2 3
/ \
4 5
as "[1,2,3,null,null,4,5]"
Clarification: The above 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.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
Solution:
1 take BST for examle, can't use inorder traversal. can't determine which node is root. the root is not the mid node.
the root is not the mid node
1
\
2
\
3
\
4
2 use preorder traversal. need to add "null" into String when the node is null. with the "null", 135 can reconstruct into many trees.
the root is not the mid node
1 3 1 \ / \ \
3 1 5 5
\ /
5 3
3 the node value is seperated by ",".
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if(root == null){
return "null,";
}
StringBuilder sb = new StringBuilder();
sb.append(root.val+",");
sb.append(serialize(root.left));
sb.append(serialize(root.right));
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] tokens = data.split(",");
int[] index = new int[]{0};
return deserialize(tokens,index);
}
Read next item from tokens. public TreeNode deserialize(String[] tokens,int[] index) { int i = index[0]; if(tokens[i].equals("null")){ index[0]++; return null; } TreeNode root = new TreeNode(Integer.parseInt(tokens[i])); index[0]++; root.left = deserialize(tokens,index); root.right = deserialize(tokens,index); return root; } }
test case 1:
1
/ \
2 3
/ \ / \
4 n n 5
n n n n
Solution:
1) serialize
when node is null, append the node into StringBuilder. don't append left and right, because we don't get right and left of the parent node which is null.
when node is not null, append the left and right into the q(even the left and right is null). append the value into the StringBuilder.
2) deserialize
start from first token to create the root node,and add this node into the q.
start a BFS traversal to rebuild the tree.
a iterative use increasing index to scan two elements each time from string tokens to create two child node of the node.
In each time of BFS traversal, get parent node from queue, create two child node from string scanning. if the child node is not null, add into queue.
There are two iterative.
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(root);
StringBuilder sb = new StringBuilder();
while(!q.isEmpty()){
TreeNode node = q.poll();
if(node!=null){
sb.append(node.val+",");
q.add(node.left);
q.add(node.right);
}else{
sb.append("null,");
}
}
return sb.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] tokens = data.split(",");
int index = 0;
TreeNode root = createNewNode(tokens[index]);
index++;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.add(root);
while(!q.isEmpty()){
TreeNode node = q.poll();
if(index >= tokens.length){
break;
}
node.left = createNewNode(tokens[index]);
index++;
node.right = createNewNode(tokens[index]);
index++;
if(node.left != null){
q.add(node.left);
}
if(node.right != null){
q.add(node.right);
}
}
return root;
}
private TreeNode createNewNode(String val){
if(val.equals("null")){
return null;
}
TreeNode node = new TreeNode(Integer.parseInt(val));
return node;
}
Comments
Post a Comment