C++ 28ms soluton, Used the same encoding scheme Leetcode uses to serialize/deserialize tree problem I/O
Encoding scheme\nFor below tree\n\n    2\n   / \\\n  1   4\n     / \\\n    3   5\n\nAfter serialization\n\n2,1,4,#,#,3,5\n\n#### Implementation\n\n\n/**\n * Def
#### Encoding scheme\nFor below tree\n```\n    2\n   / \\\n  1   4\n     / \\\n    3   5\n```\nAfter serialization\n```\n2,1,4,#,#,3,5\n```\n#### Implementation\n\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 {\n    string getNextNode(string const& data, int& offset) {\n        int end = data.find(',', offset);\n        if(end == string::npos) {\n            end = data.length();\n        }\n        string sNodeValue = data.substr(offset, end - offset);\n        offset = end + 1;\n        \n        return sNodeValue;\n    }\npublic:\n\n    // Encodes a tree to a single string.\n    string serialize(TreeNode* root) {\n        string result = "";\n        if(!root) return result;\n        queue <TreeNode*> Q;\n        Q.push(root);\n        result += to_string(root->val);\n        result += ',';\n        while(!Q.empty()) {\n            TreeNode* node = Q.front();\n            if(node->left) {\n                result += to_string(node->left->val);\n                result += ',';\n                Q.push(node->left);\n            } else {\n                result += "#,";\n            }\n            if(node->right) {\n                result += to_string(node->right->val);\n                result += ',';\n                Q.push(node->right);\n            } else {\n                result += "#,";\n            }\n            Q.pop();\n        }\n        // triming , and # from end\n        int i = result.size() - 1;\n        for(--i; i >= 0 and result[i] == '#'; i -= 2);\n        return result.substr(0, i + 1);\n    }\n\n    // Decodes your encoded data to tree.\n    TreeNode* deserialize(string data) {\n        TreeNode* root = nullptr;\n        if(data.empty()) return root;\n        queue<TreeNode*> Q;\n        int offset = 0;\n        int nodeValue = stoi(getNextNode(data, offset));\n        root = new TreeNode(nodeValue);\n        Q.push(root);\n        while(!Q.empty()) {\n            TreeNode* node = Q.front();\n            if(offset < data.length()) {\n                string sValue = getNextNode(data, offset);\n                if(sValue != "#") {\n                    int leftNodeValue = stoi(sValue);\n                    node->left = new TreeNode(leftNodeValue);\n                    Q.push(node->left);  \n                }\n            }\n            if(offset < data.length()) {\n                string sValue = getNextNode(data, offset);\n                if(sValue != "#") {\n                    int rightNodeValue = stoi(sValue);\n                    node->right = new TreeNode(rightNodeValue);\n                    Q.push(node->right);  \n                }\n            }\n            Q.pop();\n        }\n        return root;\n    }\n};\n\n// Your Codec object will be instantiated and called as such:\n// Codec codec;\n// codec.deserialize(codec.serialize(root));\n```
[]