Serialize & Deserialize BST & BT
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/\nhttps://leetcode.com/problems/serialize-and-deserialize-bst/\n\nIn my opinion, although th
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/\nhttps://leetcode.com/problems/serialize-and-deserialize-bst/\n\nIn my opinion, although the first one is marked as hard, it is actually easier than the second one. Because for the second one, you need to utilize the binary search tree property and make the tree COMPACT as required by the problem. By compact it means you can\'t append "null" values to indicate tree structure. Instead, you need to utilize bst properties to get tree structure.\n\nSolution for BST:\n```\npublic class Codec {\n    int index = 0;\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        if (root == null) {\n            return "";\n        }\n        \n        //post order traveral: left, right, root\n        StringBuilder sb = new StringBuilder();\n        \n        //left \n        String left = serialize(root.left);\n        if (left.length() > 0) {\n            sb.append(left).append(",");\n        }\n        \n        //right\n        String right = serialize(root.right);\n        if (right.length() > 0) {\n            sb.append(right).append(",");\n        }\n        \n        //root\n        sb.append(root.val).append(",");\n\n        String result = sb.substring(0, sb.length() - 1).toString();\n        return result;\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        if (data.length() <= 0) {\n            return null;\n        }\n        String[] array = data.split(",");\n        index = array.length - 1;\n        return helper(array, Integer.MIN_VALUE, Integer.MAX_VALUE);\n    }\n        \n    public TreeNode helper(String[] array, int lower, int upper){\n        if (index < 0) {\n            return null;\n        }\n        int num = Integer.valueOf(array[index]);\n        if (num < lower || num > upper) {\n            return null;\n        }\n        index--;\n        TreeNode root = new TreeNode(num);\n        root.right = helper(array, num, upper);\n        root.left = helper(array, lower, num);\n        return root;\n    }\n}\n```\n\nSolution for BT (Not this problem):\n```\npublic class Codec {\n    int index = 0;\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        if (root == null) {\n            return "null";\n        }\n        \n        StringBuilder sb = new StringBuilder();\n        \n        //preorder traversal\n        sb.append(root.val)\n            .append(",")\n            .append(serialize(root.left))\n            .append(",")\n            .append(serialize(root.right));\n\n        return sb.toString();\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        String[] array = data.split(",");\n        return helper(array);\n    }\n    \n    public TreeNode helper(String[] array){\n        if (array[index].equals("null")) {\n            index++;\n            return null;\n        }\n        TreeNode root = new TreeNode(Integer.valueOf(array[index++]));\n        root.left = helper(array);\n        root.right = helper(array);\n        return root;\n    }\n}\n```
[]