Python Solution with assertion.


  • 0
    S
    class Solution(object):
        def isValidSerialization(self, preorder):
            """
            :type preorder: str
            :rtype: bool
            """
            nodes = preorder.split(',')
            self.index = -1
    
            def preorder_traverse():
                self.index += 1
                if nodes[self.index] == '#': return None
                left = preorder_traverse()
                right = preorder_traverse()
                assert (not left and not right)
    
            try:
                preorder_traverse()
                return self.index == len(nodes) - 1
            except:
                return False

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.