A Simple Solution with Only 12 Lines of Code Beats 98%
This solution utilizes a small array to hold the position of current value, which simplifies the code to the minimum.\n\n\npublic class Codec {\n\n    // Encode
This solution utilizes a small array to hold the position of current value, which simplifies the code to the minimum.\n\n```\npublic class Codec {\n\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        if(root == null) return "#";\n        \n        return root.val + "," + serialize(root.left) + "," + serialize(root.right);\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        String[] values = data.split(",");\n        \n        return deserialize(values, new int[1]);\n    }\n    \n    private TreeNode deserialize(String[] values, int[] pos){\n        if("#".equals(values[pos[0]])){\n            pos[0]++;\n            return null;\n        }\n        \n        TreeNode cur = new TreeNode(Integer.valueOf(values[pos[0]]));\n        pos[0]++;\n        \n        cur.left = deserialize(values, pos);\n        cur.right = deserialize(values, pos);\n        \n        return cur;\n    }\n}\n\n```
[]