Serialize and Deserialize Binary Tree
Description
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called ‘serialization’ and reading back from the file to reconstruct the exact same binary tree is ‘deserialization’.
There is no limit on how to serialize or deserialize a binary tree,you just need to ensure the binary tree can be serialized to a string,and the string can be deserialized to original binary tree.
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
boolean isLeft = true;
TreeNode root = null;
/**
* This method will be invoked first, you should design your own algorithm
* to serialize a binary tree which denote by a root node to a string which
* can be easily deserialized by your own "deserialize" method later.
*/
public String serialize(TreeNode root) {
// write your code here
StringBuilder builder = new StringBuilder();
//ArrayList<Integer> results = new ArrayList<>() ;
if(root == null){
//return results ;
return builder.toString();
}
Queue<TreeNode> queue = new LinkedList<TreeNode>() ;
queue.offer(root) ;
while(!queue.isEmpty()){
TreeNode top = queue.poll();
if(top == null){
builder.append('#');
builder.append(',');
}
else{
builder.append(top.val);
builder.append(',');//以,作为分割
queue.offer(top.left);
queue.offer(top.right);
}
}
/*while(!queue.isEmpty()){
// ArrayList<Integer>
int size = queue.size() ;
for(int i=0 ; i <size ; i++){
TreeNode node = queue.poll() ;
// results.add(node.val) ;
builder.append(node.val);
builder.append(',');
if(node.left != null){
queue.offer(node.left) ;
//builder.append(node.left);
//builder.append(',');
}
if(node.left == null){
//queue.offer('#') ;
builder.append('#');
builder.append(',');
}
if(node.right != null){
queue.offer(node.right) ;
}
if(node.right == null){
//queue.offer('#') ;
builder.append('#');
builder.append(',');
}
}
}*/
return builder.toString();
}
/**
* This method will be invoked second, the argument data is what exactly
* you serialized at method "serialize", that means the data is not given by
* system, it's given by your own serialize method. So the format of data is
* designed by yourself, and deserialize it here as you serialize it in
* "serialize" method.
*/
public TreeNode deserialize(String data) {
// write your code here
String[] strs = data.split(",");
int size = strs.length;
for(int i = 0;i<size;i++){
String curstr = strs[i];
if(curstr.length() == 0)
continue;
else if(curstr.equals("#")){
appendNodeToTree(null);
}else{
int val = Integer.parseInt(curstr);
appendNodeToTree(new TreeNode(val));
}
}
return root;
}
private void appendNodeToTree(TreeNode node){
if(root == null){ //panduan root if null
root = node;
queue.offer(node);
return;
}
TreeNode top = queue.peek();
if(isLeft){
top.left = node;
}else{
top.right = node;
queue.poll();
}
isLeft = !isLeft;//取反
if(node != null)
queue.offer(node);
}
}