【题目】
【代码】

class Solution:
def isValidSerialization(self, preorder: str) -> bool:
queue=[]
c=preorder.split(",")
if len(c)%2==0 or (preorder[0]=="#" and len(preorder)>1):
return False
cnt=0
while c:
temp=c.pop(0)
queue.append(temp)
if len(queue)>=3 and queue[-1]==queue[-2] and queue[-1]=="#":
while len(queue)>=3 and queue[-1]==queue[-2] and queue[-1]=="#":
queue.pop(-1)
queue.pop(-1)
queue.pop(-1)
if queue:
queue.append("#")
return queue==[] or queue==['#']
本文介绍了一种通过字符串序列化二叉树的方法,并提供了一个用于验证序列化字符串是否有效的Python实现。该方法使用广义二叉树的概念,通过递归判断序列化字符串的有效性。
767

被折叠的 条评论
为什么被折叠?



