Python3 | O(n) Time and O(n) space
The idea is to pop the element from the stack if you get the count of the element as 0.\n\nStack is storing the node and number of remaining branch that need to
The idea is to pop the element from the stack if you get the count of the element as 0.\n\nStack is storing the node and number of remaining branch that need to discover.\nExample [2,2] will refer a node having value 2 and we did not traverse any of the branch (left or right) but at the moment we traverse the left branch that value will change to [2,1] and after traversing through right branch it will convert into [2,0].\n\n```\nclass Solution:\n    def isValidSerialization(self, preorder: str) -> bool:\n        stack = []\n        items = preorder.split(",")\n        for i, val in enumerate(items):\n            if i>0 and not stack:\n                return False\n            if stack:\n                stack[-1][1] -= 1\n                if stack[-1][1] == 0:\n                    stack.pop()\n            if val != "#":\n                stack.append([val, 2])\n        return not stack\n ```\n \n If you find this good please upvote or like, and if you feel any trouble to understand then please comment here.\n Thanks
['Stack', 'Python3']