Simple C++ Code | Preorder Traversal | Queue | Easy to understand
\nclass Codec {\npublic:\n\n    // Encodes a tree to a single string.\n    string serialize(TreeNode* root) {\n        if(!root) return "x,";\n        string l=
```\nclass Codec {\npublic:\n\n    // Encodes a tree to a single string.\n    string serialize(TreeNode* root) {\n        if(!root) return "x,";\n        string l=serialize(root->left);\n        string r=serialize(root->right);\n        return to_string(root->val)+","+l+r;\n    }\n\n    // Decodes your encoded data to tree.\n    TreeNode* deserialize(string data) {\n        queue<string> q;\n        string s="";\n        for(int i=0;i<data.size();i++){\n            if(data[i]==\',\'){\n                q.push(s);\n                s="";\n                continue;\n            }\n            s+=data[i];\n        }\n        return helper(q);\n    }\n    \n    TreeNode* helper(queue<string> &q){\n        string ch=q.front();\n        q.pop();\n        if(ch=="x") return NULL;\n        TreeNode* root=new TreeNode(stoi(ch));\n        root->left=helper(q);\n        root->right=helper(q);\n        return root;\n    }\n};\n```
['Recursion', 'Queue', 'C']