Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goish135/4a73d97ce335586c8ea89a3bf1780067 to your computer and use it in GitHub Desktop.
Save goish135/4a73d97ce335586c8ea89a3bf1780067 to your computer and use it in GitHub Desktop.
class Solution:
def isValidSerialization(self, preorder: str) -> bool:
#print(preorder)
# 一個element一個element加
## 過程中 遇到 (number # #) => #
## 如果都沒有 (number # #),則繼續一個 element 一個 element 加
stack_list=preorder.split(",")
print(stack_list)
stack_list.reverse()
another_stack = []
cnt=0
while len(stack_list)!=0:
tmp=stack_list.pop()
another_stack.append(tmp)
while len(another_stack)>=3:
rc=another_stack[-3:]
#print(rc[0],rc[1],rc[2])
if rc[0]!="#" and rc[1]=="#" and rc[2]=="#":
another_stack.pop(),another_stack.pop(),another_stack.pop()
another_stack.append("#")
else:
break
print(another_stack)
if len(another_stack)==1 and another_stack[0]=="#":
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment