# Q62：序列化二叉树

import java.awt.List;
import BinaryTree.BinaryTreeNode;
public class Q62序列化二叉树 {
/**
* 题目：序列化二叉树
* 题目说明：请实现两个函数，分别用来序列化和反序列化二叉树。
* 解题思路：区分一下序列化和反序列化：把对象转换为字节序列的过程称为对象的序列化。把字节序列恢复为对象的过程称为对象的反序列化。
*/
public static void main(String[] args) {
BinaryTreeNode root = new BinaryTreeNode();
BinaryTreeNode node1 = new BinaryTreeNode();
BinaryTreeNode node2 = new BinaryTreeNode();
BinaryTreeNode node3 = new BinaryTreeNode();
BinaryTreeNode node4 = new BinaryTreeNode();
BinaryTreeNode node5 = new BinaryTreeNode();
root.leftNode = node1;
root.rightNode = node2;
node1.leftNode = node3;
node2.leftNode  = node4;
node2.rightNode = node5;
root.value = 1;
node1.value = 2;
node2.value = 3;
node3.value = 4;
node4.value = 5;
node5.value = 6;
Q62序列化二叉树  test = new Q62序列化二叉树();
System.out.println(test.Serialize(root));
//System.out.println();
//String string = "1,2,4,$,$,$,3,5,$,$,6,$,$"; test.Deserialize(test.Serialize(root)); } public String Serialize(BinaryTreeNode root){//序列化，递归将对象转化为字节序列 StringBuilder stringBuilder = new StringBuilder(); if(root == null){ stringBuilder.append("$,");
//System.out.print("$" + ","); return stringBuilder.toString(); } //System.out.print(root.value + ","); stringBuilder.append(root.value +","); stringBuilder.append(Serialize(root.leftNode)); stringBuilder.append(Serialize(root.rightNode)); return stringBuilder.toString(); } int point = -1; public BinaryTreeNode Deserialize(String str){//反序列化，将字节序列转化为对象 point ++; if(point >= str.length()){ return null; } BinaryTreeNode node = null; String[] strArray = str.split(",");//以逗号分隔形成数组 if(!strArray[point].equals("$")){
node = new BinaryTreeNode();//二叉树的结点对象
node.value = Integer.valueOf(strArray[point]);//将字符转化成value值
node.leftNode = Deserialize(str);
node.rightNode = Deserialize(str);
System.out.print(node.value +",");
}
return node;
}
}


#### 剑指offer（64）：序列化二叉树

2016-05-01 16:23:21

#### 剑指offer（C++）——序列化二叉树

2017-04-19 09:52:59

#### 二叉树4：二叉树的序列化和反序列化

2017-05-02 22:32:55

#### Python：序列化二叉树

2017-07-14 18:42:55

#### 【剑指Offer】序列化二叉树 解题报告（Python）

2018-03-28 11:44:07

#### 二叉树的序列化和反序列化java借助队列实现

2017-01-01 22:39:04

#### 剑指offer：序列化二叉树（Python）

2018-04-02 11:56:14

#### 二叉树的序列化与反序列化

2015-01-04 19:15:28

#### 二叉树怎样序列化才能重建

2017-04-19 20:30:04

#### LeetCode 331. Verify Preorder Serialization of a Binary Tree（校验二叉树的前序遍历序列化）

2016-04-28 07:36:08