Simple Python solution


  • 0
    C
    class Solution(object):
    	def isValidSerialization(self, preorder):
    		"""
    		:type preorder: str
    		:rtype: bool
    		"""
    		count = 1
    		l = preorder.split(',')
    		i = 0
    		while i < len(l):
    			if l[i] == '#':
    				count -= 1
    			else :
    				count += 1
    			if i != len(l)-1 and count == 0 : return False
    			i += 1
    
    		return count == 0
    

Log in to reply
 

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