331. Verify Preorder Serialization of a Binary Tree (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution(object):
    def isValidSerialization(self, preorder):
        """
        :type preorder: str
        :rtype: bool
        """
        arr = preorder.split(",")
        n = 1
        for c in arr:
            if n == 0:
                return False
            if c == "#":
                n -= 1
            else:
                n += 1
        return n == 0