Simple Python Stack Solution


  • 0
    class Solution(object):
        def isValidSerialization(self, preorder):
            stack = []
            for x in preorder.split(','):
                stack.append(x)
                while len(stack) >=3 and stack[-1] == stack[-2] == '#':
                    if stack[-3] == '#':
                        return False
                    stack = stack[:-3] + ['#']
            return stack == ['#']

Log in to reply
 

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