题目
代码
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
private int index=0;
String Serialize(TreeNode root) {
if(root==null){
return "#,";
}
StringBuilder sb = new StringBuilder();
sb.append(root.val+",");
sb.append(Serialize(root.left));
sb.append(Serialize(root.right));
return sb.toString();
}
TreeNode Deserialize(String str) {
if(str==null||str.length()<=0){
return null;
}
String[] strs=str.split(",");
return DesrializeCore(strs);
}
TreeNode DesrializeCore(String[] strs){
if(strs[index].equals("#")){
return null;
}
TreeNode temp=new TreeNode(Integer.parseInt(strs[index]));
++index;
temp.left=DesrializeCore(strs);
++index;
temp.right=DesrializeCore(strs);
return temp;
}
}