序列化:前序遍历二叉树存入字符串中;反序列化:根据前序遍历重建二叉树
public class Solution { String Serialize(TreeNode root) { StringBuffer sb = new StringBuffer(); if(root == null){ sb.append("#,"); return sb.toString(); } sb.append(root.val+","); sb.append(Serialize(root.left)); sb.append(Serialize(root.right)); return sb.toString(); } public int index = -1; TreeNode Deserialize(String str) { index++; int len = str.length(); String[] strs = str.split(","); TreeNode node = null; if(index >= len) return null; if(!strs[index].equals("#")){ node = new TreeNode(Integer.valueOf(strs[index])); node.left = Deserialize(str); node.right = Deserialize(str); } return node; } }