剑指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

相关推荐

头像
昨天 15:46
已编辑
中南大学 后端
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
三年之期已到我的offer快到碗里来:9硕都比不上9本
点赞 评论 收藏
分享
喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务