[C++] O(1) Space  - Clean & Concise with Explanation
Solution Approach\n\n1. \',\'(comma) divides the string into \'#\' and node values.\n2. Try to separate them and check for string with no \',\' and only \'#\' a
### Solution Approach\n\n1. \',\'(comma) divides the string into \'#\' and node values.\n2. Try to separate them and check for string with no \',\' and only \'#\' and integers(node values)\n3. Now, the approach is to count number of leaf node this string has.\n4. Atlast, we should end with total count == 0. This helps us to know that the preorder serialization was perfect and thus return true. Otherwise, return false.\n\n#### *Code*\n```\nclass Solution {\npublic:\n    bool isValidSerialization(const string& preorder) {  \n        // Capacity is number of leaf node needed in valid tree.\n        \n        // Initialy tree can have 1 node (root) either null or value.\n        int capacity = 1;\n        stringstream ss(preorder); \n        string node;\n        while(getline(ss, node, \',\')) {\n            // We enter in while loop means there is a value either node or leaf.\n            // So we need 1 less leaf for above tree\n            capacity--;\n            \n            // Number of leaf needed is negetive is not posible.\n            if(capacity < 0)\n                return false;\n            \n            // If we encounter another node than it has 2 more leafs so adding 2 in capacity.\n            if(node != "#")\n                capacity += 2;\n        }\n        \n        // Final capacity should be zero for valid tree.\n        return capacity == 0; \n    }    \n};\n```\n\n**Time Complexity - O(N)**\n**Space Complexity - O(N) -> For copy in stringstream**\n*Note - You can iterate for input and find comma separator for O(1) space*\nCode for O(1) space approach in the comments below.\n\n**Feel free to share your ideas or any improvements as well**
['C', 'C++']