the General Solution for Serialize and Deserialize BST and Serialize and Deserialize BT
BST\nuse  upper and lower boundaries to check whether we should add null\njava\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     i
### BST\nuse  upper and lower boundaries to check whether we should add `null`\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode(int x) { val = x; }\n * }\n */\npublic class Codec {\n\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        StringBuilder sb = new StringBuilder();\n        serialize(root, sb);\n        return sb.toString();\n    }\n    \n    public void serialize(TreeNode root, StringBuilder sb) {\n        if (root == null) return;\n        sb.append(root.val).append(",");\n        serialize(root.left, sb);\n        serialize(root.right, sb);\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        if (data.isEmpty()) return null;\n        Queue<String> q = new LinkedList<>(Arrays.asList(data.split(",")));\n        return deserialize(q, Integer.MIN_VALUE, Integer.MAX_VALUE);\n    }\n    \n    public TreeNode deserialize(Queue<String> q, int lower, int upper) {\n        if (q.isEmpty()) return null;\n        String s = q.peek();\n        int val = Integer.parseInt(s);\n        if (val < lower || val > upper) return null;\n        q.poll();\n        TreeNode root = new TreeNode(val);\n        root.left = deserialize(q, lower, val);\n        root.right = deserialize(q, val, upper);\n        return root;\n    }\n}\n\n// Your Codec object will be instantiated and called as such:\n// Codec codec = new Codec();\n// codec.deserialize(codec.serialize(root));\n```\n\n### Binary Tree\nuse `#` whether we should add `null`\n```java\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode(int x) { val = x; }\n * }\n */\npublic class Codec {\n\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        StringBuilder sb = new StringBuilder();\n        serialize(root, sb);\n        return sb.toString();\n    }\n    \n    public void serialize(TreeNode root, StringBuilder sb) {\n        if (root == null) {\n            sb.append("#").append(",");\n        } else {\n            sb.append(root.val).append(",");\n            serialize(root.left, sb);\n            serialize(root.right, sb);\n        }\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        Queue<String> q = new LinkedList<>(Arrays.asList(data.split(",")));\n        return deserialize(q);\n    }\n    \n    public TreeNode deserialize(Queue<String> q) {\n        String s = q.poll();\n        if (s.equals("#")) return null;\n        TreeNode root = new TreeNode(Integer.parseInt(s));\n        root.left = deserialize(q);\n        root.right = deserialize(q);\n        return root;\n    }\n    \n}\n\n// Your Codec object will be instantiated and called as such:\n// Codec codec = new Codec();\n// codec.deserialize(codec.serialize(root));\n```\n\n
[]