Simple solution using level order traversal.
\n# Complexity\n- Time complexity:O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(N)\n Add your space complexity here, e.g. O(n) \n\n# 
\n# Complexity\n- Time complexity:$$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Codec {\npublic:\n\n    // Encodes a tree to a single string.\n    string serialize(TreeNode* root) {\n        if(root == NULL) return "";\n\n        string s = "";\n        queue<TreeNode*>q;\n        q.push(root);\n        while(!q.empty()){\n            TreeNode *currNode = q.front();\n            q.pop();\n            if(currNode == NULL) s.append("#,");\n            else s.append(to_string(currNode->val) + \',\');\n            if(currNode != NULL){\n                q.push(currNode->left);\n                q.push(currNode->right);\n            }\n        }\n        return s;\n    }\n\n    // Decodes your encoded data to tree.\n    TreeNode* deserialize(string data) {\n        if(data.size() == 0) return NULL;\n        stringstream  s(data);\n        string str;\n        getline(s,str,\',\');\n        TreeNode*root = new TreeNode(stoi(str));\n        queue<TreeNode*>q;\n        q.push(root);\n        while(!q.empty()){\n            TreeNode * node = q.front();\n            q.pop();\n\n            getline(s,str,\',\');\n            if(str == "#"){\n                node->left = NULL;\n            }\n            else{\n                TreeNode* leftNode = new TreeNode(stoi(str));\n                node->left = leftNode;\n                q.push(leftNode);\n            }\n            getline(s,str,\',\');\n            if(str == "#"){\n                node->right = NULL;\n            }\n            else{\n                TreeNode* rightNode = new TreeNode(stoi(str));\n                node->right = rightNode;\n                q.push(rightNode);\n            }\n        }\n        return root;\n    }\n};\n\n// Your Codec object will be instantiated and called as such:\n// Codec* ser = new Codec();\n// Codec* deser = new Codec();\n// string tree = ser->serialize(root);\n// TreeNode* ans = deser->deserialize(tree);\n// return ans;\n```
['C++']