题目描述:请实现两个函数,分别用来序列化和反序列化二叉树
实现语言:Java
/* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ 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 Integer index=-1; TreeNode Deserialize(String str) { ++index; if(str.length()<=0){ return null; } String[] s=str.split(","); TreeNode node=null; if(!s[index].equals("#")){ node=new TreeNode(Integer.valueOf(s[index])); node.left=Deserialize(str); node.right=Deserialize(str); } return node; } }