题目描述
请实现两个函数,分别用来序列化和反序列化二叉树
public class Solution {
//加static就错
public StringBuilder s = new StringBuilder();
public int start = 0;
String Serialize(TreeNode root) {
if(root==null){
s.append("#,");
return "#,";
}
s.append(root.val+",");
Serialize(root.left);
Serialize(root.right);
return s.toString();
}
TreeNode Deserialize(String str) {
if(str==""){
return null;
}
String[] cc = str.split(",");
return Deserialize(cc,cc.length-1);
}
TreeNode Deserialize(String[] cc,int end) {
if(start>end){
return null;
}
if(cc[start].equals("#")){
start++;
return null;
}
TreeNode root = new TreeNode(Integer.valueOf(cc[start]));
start++;
if(start<=end){
root.left=Deserialize(cc,end);
root.right=Deserialize(cc,end);
}
return root;
}
}