Serialize and Deserialize BST | Java | Preorder Traversal
```\n\npublic class Codec {\n\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        \n        if(root==null) return 
```\n\npublic class Codec {\n\n    // Encodes a tree to a single string.\n    public String serialize(TreeNode root) {\n        \n        if(root==null) return "";\n        \n        List<String> data = new ArrayList<>();\n        preorderTraversal(root, data);\n        \n        return String.join(",", data);\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        \n        if(data.length()==0) return null;\n        \n        String[] nodes= data.split(",");\n        \n        return convertPreorderTree( nodes, 0, nodes.length-1);\n    }\n    \n    private void preorderTraversal(TreeNode root, List<String> data){\n        \n        if(root==null) return;\n        \n        data.add(""+root.val);\n        \n        preorderTraversal(root.left, data);\n        preorderTraversal(root.right, data);\n    }\n    \n    private TreeNode convertPreorderTree(String[] preorder, int start, int end){\n        \n        if(start > end) return null;\n        \n        TreeNode node= new TreeNode(Integer.parseInt(preorder[start]));\n        \n        int i= start+1;\n        while(i<=end && Integer.parseInt(preorder[i]) < Integer.parseInt(preorder[start])){\n            i++;\n        }\n        \n        node.left = convertPreorderTree(preorder, start+1, i-1);\n        node.right = convertPreorderTree(preorder, i, end);\n        \n        return node;\n    }\n}\n\n
[]