/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
import java.util.LinkedList;
import java.util.Arrays;
public class Solution {
    String Serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        return rSerialize(root, sb).toString();
    }
    TreeNode Deserialize(String s) {
        String[] arr = s.split(",");
        LinkedList<String> lst = new LinkedList<String>(Arrays.asList(arr));
       return rDeserialize(lst);
    }
    StringBuilder rSerialize(TreeNode root, StringBuilder s) {
        if(root == null) {
            s.append("#,");
        } else {
            s.append(root.val + ",");
            rSerialize(root.left, s);
            rSerialize(root.right, s);
        }
        return s;
    }
    TreeNode rDeserialize(LinkedList<String> lst) {
        if(lst.size() <= 0) {
            return null;
        }
        if(lst.getFirst().equals("#")) {
            lst.removeFirst();
            return null;
        }
        TreeNode root = new TreeNode(Integer.parseInt(lst.removeFirst()));
        root.left = rDeserialize(lst);
        root.right = rDeserialize(lst);
        return root;
    }
}