C++ O(N) Solution
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Verify Preorder Serialization of a Binary Tree.\nMemory Usage: 7 MB, less than 43.37% of C++ on
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Verify Preorder Serialization of a Binary Tree.\nMemory Usage: 7 MB, less than 43.37% of C++ online submissions for Verify Preorder Serialization of a Binary Tree.\n\nhttps://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/discuss/473002/0-ms-C%2B%2B-simple-solution-with-counter\n\n```\nclass Solution {\npublic:\n    bool isValidSerialization(string preorder) {\n        \n        int count = 0;\n        int n = preorder.length();\n        for(int i=0;i<n;i++)\n        {\n            if(preorder[i]==\',\')continue;\n            \n            // count<0 happens when node has no parent EX => ["#,1,#"]\n            if(count<0) return false;\n            \n            // decrease count as we find NULL\n            if(preorder[i]==\'#\')count--;\n            else\n            {\n                while(i<n && preorder[i]!=\',\')i++;\n                \n                // increase count as there is a node\n                count++;\n            }            \n        }\n        \n        // Valid preorder would have N node and N+1 NULL nodes and count would be -1\n        return (count==-1);\n    }\n};\n```
['C']