Verify Preorder Serialization of a Binary Tree

Solution Code
class Solution:
    def isValidSerialization(self, preorder: str) -> bool:
        slots = 1
        for node in preorder.split(','):
            slots -= 1
            if slots < 0:
                return False
            if node != '#':
                slots += 2
        return slots == 0
Video GuideLeetcode Daily

Time Complexity

O(N)

Space Complexity

O(N)