剑指offer序列化与反序列化,VSCode和平台结果不同

大佬们,剑指offer序列化与反序列化那道题我是这么做的:
class Solution:
def Serialize(self, root):
# write code here
if not root:
return ""
self.number = []
self.cur = []
self.number.append(root)
self.Se(root)
#print(self.cur)
return ",".join(self.cur)
def Se(self,root):
while(self.number):
if self.number[0] != "#" and self.number[0]:
self.cur.append(str(self.number[0].val))#AttributeError: 'NoneType' object has no attribute 'val',number[0]==None
if self.number[0].left:
self.number.append(self.number[0].left)
if self.number[0].right:
self.number.append(self.number[0].right)#没有判断语句,一直循环,self.number[None]
self.number.pop(0)

if len(self.number)>0 and self.number[0]=="#":
self.cur.append("#")#IndexError: list index out of range
self.number.pop(0)
#if not self.number[0]:

def Deserialize(self, s):
# write code here
if not s:
return
l = s.split(",")
root = TreeNode(None)
return(self.listCreatTree(root,l,0))
def listCreatTree(self, root,l,i):
if i < len(l):
val = l[i]
if val != "#":
root = TreeNode(val)
root.left = self.listCreatTree(root, l, 2*i+1)
root.right = self.listCreatTree(root, l, 2*i+2)
return root
if val == "#":
root = TreeNode(val)
return root
报错:{%d format: a number is required, not str是哪的问题啊,我在VSCode上跑没有问题的啊
求大佬告知(有答案,自己觉得思路这样走想多一种思路)
#Python#
全部评论
在VSCode平台上全代码: class TreeNode:     def __init__(self, x):         self.val = x         self.left = None         self.right = None class Solution:         def Serialize(self, root):         # write code here         if not root:             return ""         self.number = []         self.cur = []         self.number.append(root)         self.Se(root)         #print(self.cur)         return ",".join(self.cur)     def Se(self,root):         while(self.number):             if self.number[0] != "#" and self.number[0]:                 self.cur.append(str(self.number[0].val))#AttributeError: 'NoneType' object has no attribute 'val',number[0]==None                 if self.number[0].left:                     self.number.append(self.number[0].left)                 if self.number[0].right:                     self.number.append(self.number[0].right)#没有判断语句,一直循环,self.number[None]                 self.number.pop(0)             if len(self.number)>0 and self.number[0]=="#":                 self.cur.append("#")#IndexError: list index out of range                 self.number.pop(0)             #if not self.number[0]:     def Deserialize(self, s):         # write code here         if not s:             return         l = s.split(",")         root = TreeNode(None)         return(self.listCreatTree(root,l,0))     def listCreatTree(self, root,l,i):         if i < len(l):             val = l[i]             if val != "#":                 root = TreeNode(val)                 root.left = self.listCreatTree(root, l, 2*i+1)                 root.right = self.listCreatTree(root, l, 2*i+2)                 return root             if val == "#":                 root = TreeNode(val)                 return root         '''     flag=-1     def Serialize(self, root):         # write code here         if not root:             return '#'         return str(root.val)+','+self.Serialize(root.left)+','+self.Serialize(root.right)             def Deserialize(self, s):         # write code here         #print(s)         self.flag+=1         lis=s.split(',')                 if self.flag>=len(lis):             return None                 root=None         #print("----------",self.flag,len(lis),len(s))         if lis[self.flag]!='#':             root=TreeNode(int(lis[self.flag]))             root.left=self.Deserialize(s)             root.right=self.Deserialize(s)         return root     '''     def midTraversal(self, root):         if root:             self.midTraversal(root.left)             print(root.val)             self.midTraversal(root.right) if __name__ == "__main__":     #b = [8,6,10,5,7,9,11]     #print()     #print(list(b))     #print(b.split(","))     a = Solution()     root = TreeNode(None)     root = a.Deserialize("8,6,10,5,7,9,11")     print("+++++")     a.midTraversal(root)     print("--------------------")     print(a.Serialize(root))    
点赞 回复 分享
发布于 2019-06-29 17:58
我跟你同样问题,这个好像是python版本问题
点赞 回复 分享
发布于 2019-08-16 14:35
跟你一样,leetcode都没问题
点赞 回复 分享
发布于 2020-11-12 21:10

相关推荐

最近和朋友聊天,她说了句让我震惊的话:"我发现我连周末点外卖都开始'最优解'了,一定要赶在高峰期前下单,不然就觉得自己亏了。"这不就是典型的"班味入侵"吗?工作思维已经渗透到生活的方方面面。
小型域名服务器:啊?我一直都这样啊?我还以为是我爱贪小便宜呢?每次去实验室都得接一杯免费的开水回去,出门都得规划一下最短路径,在宿舍就吃南边的食堂,在实验室就吃北边的食堂,快递只有顺路的时候才取。
点赞 评论 收藏
分享
重生2012之我是java程序员:换个稍微正式点的照片吧
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务