C++ Simple and Short Solution, O(n) TC O(1) SC, Explained
Idea:\nWe use stringstream to split the string by commas.\nWe use nodes to count the nodes we are allowed to have next.\nEvery time we get a new node we decreas
**Idea:**\nWe use stringstream to split the string by commas.\nWe use `nodes` to count the nodes we are allowed to have next.\nEvery time we get a new node we decrease `nodes`, if we got below zero it\'s false.\nIf we get a non-null node, we can have after it two more nodes.\nAt the end, we are supposed to end up with `nodes == 0`, otherwise the tree is not good.\n```\nclass Solution {\npublic:\n    bool isValidSerialization(string preorder) {\n        stringstream ss(preorder);\n        string curr;\n        int nodes = 1;\n        while (getline(ss, curr, \',\')) {\n            nodes--;\n            if (nodes < 0) return false;\n            if (curr != "#") nodes += 2;\n        }\n        return nodes == 0;\n    }\n};\n```\n**Like it? please upvote!**
['C']