1 public class Solution { 2 public int index = -1;
3 StringBuilder s = new StringBuilder(); 4 String Serialize(TreeNode root) {
5 if(root == null){ 6 s.append("#,"); 7 return s.toString(); 8 } 9 s.append(root.val+","); 10 s.append(Serialize(root.left)); 11 s.append(Serialize(root.right)); 12 return s.toString(); 13 } 14 TreeNode Deserialize(String str) { 15 index++; 16 String[] DLRseq = str.split(","); 17 TreeNode leave = null; 18 if(!DLRseq[index].equals("#")){ 19 leave = new TreeNode(Integer.valueOf(DLRseq[index])); 20 leave.left = Deserialize(str); 21 leave.right = Deserialize(str); 22 } 23 return leave; 24 } 25 }