public class Solution {
String Serialize(TreeNode root) {
if(root == null){
return "";
}
StringBuilder sb = new StringBuilder();
Serialize1(root,sb);
return sb.toString();
}
void Serialize1(TreeNode root,StringBuilder sb) {
if(root == null){
sb.append("#,");
return;
}
sb.append(root.val);
sb.append(",");
Serialize1(root.left, sb);
Serialize1(root.right, sb);
}
TreeNode Deserialize(String str) {
if(str.length() == 0){
return null;
}
String[] string = str.split(",");
return Deserialize1(string);
}
int i = -1;
TreeNode Deserialize1(String[] str){
i++;
if(!str[i].equals("#")){
TreeNode root = new TreeNode(0);
root.val = Integer.parseInt(str[i]);
root.left = Deserialize1(str);
root.right = Deserialize1(str);
return root;
}
return null;
}
}