Short'n'Sweet Recursive Python solution, beats 97%

  • 0
    from collections import deque
    class Solution(object):
        def isValidSerialization(self, po):
            def visit(nodes):
                if not nodes:
                    return True
                node = nodes.popleft()
                l, r = True, True
                if node != "#":
                    l = visit(nodes)
                    if not nodes:
                        return False
                    r = visit(nodes)
                return l and r
            po = deque(po.split(','))
            return visit(po) and not po
    # Runtime: 50ms

Log in to reply

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