# LeetCode297.Serialization and Deserialization of Binary Trees

### Title Description

Serialization is the operation of converting a data structure or object into consecutive bits, so that the converted data can be stored in a file or in memory, but can also be transferred to another computer environment through the network, and the original data can be reconstructed in the opposite way.

Please design an algorithm to serialize and deserialize the binary tree.There is no limit to your sequence/deserialization algorithm execution logic here. You only need to ensure that a binary tree can be serialized into a string and deserialized into the original tree structure.

### Solving problems

There were so many big jobs that I didn't have time to write a title. It's good to write this one today. tag: binary tree hierarchy traversal, serialization.
Do you want to serialize a String? Make sure you iterate through it in middle order, then output it in fixed format, and then generate the tree recursively when deserialize.however(116)It's unexpected that we're traversing in sequence.Since a large number of operations are required on string composition and splitting, they are done with JAVA.It's not difficult, but I'll walk through the sequence that rarely writes deserialization.

### source code

```import java.util.LinkedList;
import java.util.Queue;

public class lc297 {

public static void main(String[] args) {
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
node1.left = node2;
node1.right = node3;
node2.left = null;
node2.right = null;
node3.left = node4;
node3.right = node5;
node4.left = null;
node4.right = null;
node5.left = null;
node5.right = null;
String ser_str = serialize(node1);
System.out.println(ser_str);
TreeNode root = deserialize(ser_str);
}

// Encodes a tree to a single string.
public static String serialize(TreeNode root) {
if (root == null)
return new String("N");

TreeNode front;
StringBuilder serial_str = new StringBuilder("" + root.val);

while(!node_que.isEmpty()){
front = node_que.remove();

if (front.left != null) {
serial_str.append(",").append(front.left.val);
}
else{
serial_str.append(",N");
}

if (front.right != null) {
serial_str.append(",").append(front.right.val);
}
else{
serial_str.append(",N");
}
}

return serial_str.toString();
}

// Decodes your encoded data to tree.
public static TreeNode deserialize(String data) {
int i = 0;
String[] elems = data.split(",");
TreeNode root = TNInitialByStr(elems[0]);
if (root == null)
return null;
TreeNode node = null;
while(!node_q.isEmpty()){
node = node_q.poll();
node.left = TNInitialByStr(elems[++i]);
node.right = TNInitialByStr(elems[++i]);
if (node.left != null)
if (node.right != null)
}
return root;
}

public static TreeNode TNInitialByStr(String val){
if(val.equals("N"))
return null;
return new TreeNode(Integer.parseInt(val));
}
//Definition for a binary tree node.
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
}

```

#### LC format

```/**
* 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 new String("N");

TreeNode front;
StringBuilder serial_str = new StringBuilder("" + root.val);

while(!node_que.isEmpty()){
front = node_que.remove();

if (front.left != null) {
serial_str.append(",").append(front.left.val);
}
else{
serial_str.append(",N");
}

if (front.right != null) {
serial_str.append(",").append(front.right.val);
}
else{
serial_str.append(",N");
}
}

return serial_str.toString();
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
int i = 0;
String[] elems = data.split(",");
TreeNode root = TNInitialByStr(elems[0]);
if (root == null)
return null;
TreeNode node = null;
while(!node_q.isEmpty()){
node = node_q.poll();
node.left = TNInitialByStr(elems[++i]);
node.right = TNInitialByStr(elems[++i]);
if (node.left != null)
if (node.right != null)
}
return root;
}

public TreeNode TNInitialByStr(String val){
if(val.equals("N"))
return null;
return new TreeNode(Integer.parseInt(val));
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.deserialize(codec.serialize(root));
```

Tags: codec Java network

Posted on Tue, 16 Jun 2020 12:59:47 -0400 by massimoGornatti