Python recursive traversal solution


  • 0
    H
    class Solution(object):
        def traverse(self,sl):
            if not sl:
                return False
            x=sl.pop(0)
            if x=='#':
                return True
            if not self.traverse(sl):
                return False
            if not self.traverse(sl):
                return False
            return True
        def isValidSerialization(self, preorder):
            """
            :type preorder: str
            :rtype: bool
            """
            po=preorder.split(',')
            if not self.traverse(po):
                return False
            return not po

Log in to reply
 

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