Java, bfs, queue
\npublic class Codec {\n\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        StringBuilder sb = new StringBuilder(
```\npublic class Codec {\n\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        StringBuilder sb = new StringBuilder();\n        if(root ==null) return sb.toString();\n        Queue<TreeNode> q = new LinkedList<>();\n        q.add(root);\n        while(!q.isEmpty()){\n            TreeNode node = q.poll();\n            if(node !=null){\n                q.add(node.left);\n                q.add(node.right);\n                sb.append(node.val+",");\n            }else{\n                sb.append("#"+",");\n            }\n        }\n        return sb.toString();\n        \n    }\n\n    // Decodes encoded data to tree.\n    public TreeNode deserialize(String data) {\n        TreeNode head = null;\n        if(data ==null || data.length() ==0) return head;\n        String[] nodes = data.split(",");//get elements include null;\n        TreeNode[] treeNodes = new TreeNode[nodes.length];//get a TreeNode array\n        for(int i = 0; i<nodes.length; i++){\n            if(! nodes[i].equals("#") )\n                treeNodes[i] = new TreeNode(Integer.valueOf(nodes[i]));//change non-null elem into node\n        }\n        for(int i =0,j=1; j<treeNodes.length;i++){\n            if(treeNodes[i] != null){\n                treeNodes[i].left = treeNodes[j++];\n                treeNodes[i].right = treeNodes[j++];\n            }\n        }\n        return treeNodes[0];       \n    }\n}\n\n```
[]