美文网首页
331. Verify Preorder Serializati

331. Verify Preorder Serializati

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-26 13:07 被阅读0次
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

相关文章

网友评论

      本文标题:331. Verify Preorder Serializati

      本文链接:https://www.haomeiwen.com/subject/suejvttx.html