Python iterative serializatoin beats 89%

  • 0

    My code serializes two tree and checks if t is in s.

    class Solution(object):
        def isSubtree(self, s, t):
            :type s: TreeNode
            :type t: TreeNode
            :rtype: bool
            def serialize(root):
                ans = []
                stack = [(root, 1)]
                while stack:
                    node, p = stack.pop()
                    if not node:
                    if p == 0:
                        ans.append("|" + str(node.val))
                        stack.append((node.right, 1))
                        stack.append((node.left, 1))
                        stack.append((node, 0))
                return ",".join(ans)
            return serialize(t) in serialize(s)

Log in to reply

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