C++ || Full Explanation || beginner Friendly || vacancy approach || With pictorial Representation
@Iemprashant \uD83D\uDE80\n331. Verify Preorder Serialization of a Binary Tree\n\n1. C++ Code. \uD83C\uDF93\n2. Please Upvote if it helps\u2B06\uFE0F\n3. Detail
[**@Iemprashant**](https://leetcode.com/iemprashant/) \uD83D\uDE80\n[331. Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/)\n```\n1. C++ Code. \uD83C\uDF93\n2. Please Upvote if it helps\u2B06\uFE0F\n3. Detailed Approach \u2705\n4. Github Repository for code:->\n\n```\n\u26A1[ GithubRepo](https://github.com/iemprashant/LeetHub)\n \n**Observation** \uD83D\uDCCD\n```\nIdea-1=> As we start making a tree we have a single vacancy for node = root.\nIdea-2=>For a node!=null-> creates 2 vacancy left and right.\nIdea-3=>If node !=null and has left child ,so vacancy=> 1 from root that is right and 2 from root->left.\n\n```\n![image](https://assets.leetcode.com/users/images/cf9ffe40-2364-458a-9618-cf22650fd100_1647513898.4065015.png)\n\n\n**Approach** \uD83D\uDEA9 \n1:=> Now, we will check the vacancy and traverse the string.\n2:=>As string has \',\' Commas we need to split it, (we can\'t use a single char of string as token as there are numbers like 92,934 are a valid token)\n3=>We will split the token and put it into a vector.\n4=>Now traversing the tokens of vector, for every char we will fill a vacancy so reduce it by 1;\n5=>if we have vacancy less than 0, so no space left  return false,\n6=>if we have char !=\' #\' i.e not null, so  it will create 2 vacancy => left , right;\n7=>if we have char ==\' #\' null ,so no vacancy added.\n\n**CODE** \uD83D\uDCCB\n```\nclass Solution\n{\n    public:\n        bool isValidSerialization(string p)\n        {\n            //creating Tokens\n            vector<string> tokens;\n            stringstream check(p);\n            string temp;\n            while (getline(check, temp, \',\'))\n            {\n                tokens.push_back(temp);\n            }\n            int v = 1;\t//vacancy\n            //checking for valid preorder\n            for (auto s: tokens)\n            {\n                v--;\n                if(v<0){\n                    return false;\n                }\n                if (s != "#")\n                {\n                    v += 2;\n                }\n            }\n            return v == 0;\n        }\n};\n```\n\nPlease Upvote if it helps\u2B06\uFE0F
['String', 'C']