C++ 3ms solution with detailed exlpanation
\n\n\nstruct NODE{\n    int val;\n    int left = 0;\n    int right = 0;\n    NODE(int v){val = v;}\n};\nclass Solution {\npublic:\n    bool isValidSerialization
![image](https://assets.leetcode.com/users/images/920442e8-2a7c-4a7c-bfe7-00c9728f5748_1644486421.196695.png)\n\n```\nstruct NODE{\n    int val;\n    int left = 0;\n    int right = 0;\n    NODE(int v){val = v;}\n};\nclass Solution {\npublic:\n    bool isValidSerialization(string preorder) {\n        if(preorder == "#") return true;\n        if(preorder[0] == \'#\') return false;\n        /* Construction of nums array to store node values*/\n        vector<int> nums;\n        string node;\n        for(int i=0; i<preorder.size(); i++){\n            //encounter a comma\n            if(preorder[i] == \',\'){\n                //compute the node\n                if(node == "#") {nums.push_back(-1); node = "";}\n                else{\n                    int number=0;\n                    for(char &x: node){\n                        number*=10;\n                        number+=(int)(x-\'0\');\n                    }\n                    nums.push_back(number);\n                    node = "";\n                }\n                //end of this block\n            }\n            else node.push_back(preorder[i]);\n        }\n        //compute the last node because we ddn\'t encounter comma at the end\n        if(node == "#") nums.push_back(-1);\n        else{\n            int number=0;\n            for(char &x: node){\n                number*=10;\n                number+=(x-\'0\');\n            }\n            nums.push_back(number);\n        }\n        //end of this block\n        //for(int &x:nums) cout<<x<<" ";\n        /*nums array constructed*/\n        stack<NODE*> tree; //created a stack to push and pop nodes\n        NODE* root = new NODE(nums[0]);\n        tree.push(root); \n        //first pushed the root with left and right branches 0 indicating that left and right trees haven\'t completed\n        for(int i=1; i<nums.size(); i++){\n            //if both branches become 1...then we have to pop that node from stack\n            //because the subtree has completed\n            NODE* top = tree.top();\n            while(top->left and top->right) {\n                tree.pop();\n                if(tree.empty()) return false; //because the stack cannot become empty before we complete our traversal\n                top = tree.top();\n                if(!top->left) top->left = 1;\n                else top->right = 1;\n            }            \n            //if we encounter -1\n            //this means that the child branch has ended\n            //so we make branch of the top of stack as 1 which was previously 0\n            if(nums[i] == -1){\n                if(!top->left) {top->left = 1; continue;}\n                else {top->right = 1; continue;}\n            }\n            \n            //if we encounter a normal value\n            //we\'ll push the node\n            if(nums[i]>=0){\n                NODE* newnode = new NODE(nums[i]);\n                tree.push(newnode);\n            }\n        }\n        NODE* top = tree.top();\n        while(top->left and top->right) {\n            tree.pop();\n            if(tree.empty()) return true; //all the branches were traversed\n            top = tree.top();\n            if(!top->left) top->left = 1;\n            else top->right = 1;\n        }     \n        return false; //we couldn\'t fill up the tree\n    }\n};\n```
['Stack', 'C']