Emulating actual traversal in Python

  • 0

    Emulate actual pre-order traversal to verify to sequence, which can solve the same problem for in-order and post-order traversal.

    class Solution(object):
        def isValidSerialization(self, preorder):
            :type preorder: str
            :rtype: bool
            def dfs(preorder):
                if self.index >= len(preorder):
                    return False
                cur = preorder[self.index]
                self.index += 1
                if cur == "#":
                    return True
                return dfs(preorder) and dfs(preorder)
            self.index = 0
            prelist = preorder.split(',')
            if dfs(prelist) and self.index == len(prelist):
                return True
            return False

Log in to reply

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