class Solution(object):
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
#a null node use a slot
#a non-null node use a slot but create 2 slots, net gain 1 slot
nodes=preorder.split(',')
slot=1
for node in nodes:
if slot==0:return False
if node=='#':
slot-=1
else:
slot+=1
return slot==0
网友评论