6ms C++ using stack
\nclass Solution {\npublic:\n    bool isValidSerialization(string preorder) {\n        if(preorder == "#") return true;\n        stringstream ss(preorder);\n   
```\nclass Solution {\npublic:\n    bool isValidSerialization(string preorder) {\n        if(preorder == "#") return true;\n        stringstream ss(preorder);\n        stack<int>stk;\n        string s = "";\n        while(getline(ss, s, ',')){\n            if(s == "#" && stk.empty()) return false;\n            if(!stk.empty()) stk.top()++;\n            if(!stk.empty() && stk.top() == 2) stk.pop();\n            if(s != "#") stk.push(0);\n            if(stk.empty() && !ss.eof()) return false;\n        }\n        return stk.empty();\n    }\n};\n```
['C++']