Java O(N) recursive solution
```\n// Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        if (root == null) {\n            return ".";\n        }\n     
```\n// Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        if (root == null) {\n            return "*.";\n        }\n        StringBuilder sb = new StringBuilder();\n        sb.append(root.val);\n        if (root.left == null && root.right == null) {\n            sb.append("*.");\n            return sb.toString();\n        }\n        sb.append(".");\n        sb.append(serialize(root.left));\n        sb.append(serialize(root.right));\n        return sb.toString();\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        int[] begin = {0};\n        return deserializeMethod(data, begin);\n    }\n\n    private TreeNode deserializeMethod(String data, int[] begin) {\n        int index = data.indexOf(".", begin[0]);\n        TreeNode node = null;\n        if (data.charAt(index - 1) == '*') {\n            String str = data.substring(begin[0], index - 1);\n            begin[0] = index + 1;\n            if (str.equals("")) {\n                return null;\n            }\n            node = new TreeNode(Integer.parseInt(str));\n        } else {\n            String str = data.substring(begin[0], index);\n            begin[0] = index + 1;\n            node = new TreeNode(Integer.parseInt(str));\n            node.left = deserializeMethod(data, begin);\n            node.right = deserializeMethod(data, begin);\n        }\n        return node;\n    }
[]