Simple C++ solution using stack without extra splitting function
At first glance, a leaf node's pattern should look like number,#,#, start from the beginning of array, once you see this pattern, convert it to a single "#", me
 At first glance, a leaf node's pattern should look like `number,#,#`, start from the beginning of array, once you see this pattern, convert it to a single "#", meaning the node with value `number` has already been fully explored(left subtree, right subtree), so we replace it with a "#". While iterating the array, we just keep doing this kind of absorbing/merging backward until we reach the end of array. Then we check if the root has been fully explored, which should eventually be a single `#`. During absorbing, if this pattern appears `#,#,#`, return false. It's known that it's a pain in C++ that there is no split function as Java does, but it won't matter here since split string is not necessary, we just need to know before `,` it's a `number` or `#`.\n\n"9,3,4,#,#,12,#,#,2,#,6,#,#"\n\nstack status\n\n    char   stack\n    '9':   '9'  \n    '3':   '3','9'\n    '4':   '4','3','9'\n    '#':   '#','4','3','9'\n    '#':   '#','3','9'\n    '12':  'n', '#', '3','9'\n    '#':   '#','1', '#', '3','9'\n    '#':   '#','3','9' -> '#','9'\n    '2':   '2', '#','9'\n    '#':   '#', '2', '#','9'\n    '6':   '6', '#', '2','#','9'\n    '#':   '#', '6', '#', '2','#','9'\n    '#':   '#', '2','#','9' -> '#','9' -> '#'\n\nCode:\n\n    class Solution {\n    public:\n        bool isValidSerialization(string preorder) {\n            stack<char> stk;\n            bool isNum = false;\n            preorder.push_back(','); // dummy tail\n            \n            for(auto c: preorder){\n                if(c == '#'){\n                    // absorb: search for pattern `#, number` backward\n                    while(!stk.empty() && stk.top() == '#'){ \n                        stk.pop(); // pop `#`\n                        if(stk.empty() || stk.top() == '#') return false; // pattern `#,#,#`\n                        stk.pop(); // pop `number`\n                    }\n                    stk.push('#'); // replace `number` with `#` since it has been fully explored/validated\n                }else if(c == ','){\n                    if(isNum) stk.push('n'); // indicate this is a number instead of using the real number\n                    isNum = false;\n                }else{\n                    isNum = true;\n                }\n            }\n            \n            return stk.size() == 1 && stk.top() == '#';\n        }\n    };
['Binary Tree', 'C++']