The simplest python solution with explanation (no stack, no recursion)
We just need to remember how many empty slots we have during the process. \n\nInitially we have one ( for the root ). \n\nfor each node we check if we still hav
We just need to remember how many empty slots we have during the process. \n\nInitially we have one ( for the root ). \n\nfor each node we check if we still have empty slots to put it in. \n\n - a null node occupies one slot.\n - a non-null node occupies one slot before he creates two more. the net gain is one. \n\n----------\n    class Solution(object):\n        def isValidSerialization(self, preorder):\n            """\n            :type preorder: str\n            :rtype: bool\n            """\n            # remember how many empty slots we have\n            # non-null nodes occupy one slot but create two new slots\n            # null nodes occupy one slot\n            \n            p = preorder.split(',')\n            \n            #initially we have one empty slot to put the root in it\n            slot = 1\n            for node in p:\n                \n                # no empty slot to put the current node\n                if slot == 0:\n                    return False\n                    \n                # a null node?\n                if node == '#':\n                    # ocuppy slot\n                    slot -= 1\n                else:\n                    # create new slot\n                    slot += 1\n            \n            #we don't allow empty slots at the end\n            return slot==0
['Python']