Easy Java Solution || 2 Approaches Beats 100% online submissions
Approach\n1. Perform DFS and store the result in a string seperated by \',\' and null is represented by \'x\'\n2. To helpdeserialize follow the approach to buil
# Approach\n1. **Perform DFS and store the result in a string seperated by \',\' and null is represented by \'x\'**\n2. **To helpdeserialize follow the approach to build a Tree**\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N) \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n# Code\n```\nclass Codec {\n\n    public String serialize(TreeNode root) {\n        StringBuilder res = new StringBuilder();\n\n        helpserialize(root,res);\n\n        return res.toString();\n    }\n\n    private void helpserialize(TreeNode root, StringBuilder res){\n        if(root == null){\n            res.append("x,");\n            return ;\n        }\n\n        res.append(root.val);\n        res.append(\',\');\n\n        helpserialize(root.left, res);\n        helpserialize(root.right, res);\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        Deque<String> q = new LinkedList<>();\n\n        q.addAll(Arrays.asList(data.split(",")));\n\n        return helpdeserialize(q);\n    }\n\n    private TreeNode helpdeserialize(Deque<String> q){\n        String res = q.remove();\n\n        if(res.equals("x")){\n            return null;\n        }\n\n        TreeNode root = new TreeNode(Integer.parseInt(res));\n\n        root.left = helpdeserialize(q);\n        root.right = helpdeserialize(q);\n\n        return root;\n    }\n}\n```\n\n# Java O(1) Code\n\n# I mean... why not \uD83D\uDE02\n```\npublic class Codec {\n\n    static TreeNode res;\n    public String serialize(TreeNode root) {\n        res = root;\n        return "";\n    }\n\n    // Decodes your encoded data to tree.\n    public TreeNode deserialize(String data) {\n        return res;\n    }\n}\n```
['Tree', 'Depth-First Search', 'Binary Search Tree', 'Binary Tree', 'Java']