BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / python / #21134同步于 2018/3/9
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖

【问题】leetcode问题求助

crelyy
2018/3/9镜像同步2 回复
leetcode 113. Path Sum II 我的代码是: '''# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """ ret = [] st = [] self.findPaths(ret, st, root, sum) return ret def findPaths(self, ret, st, root, sum): if not root: return [] st.append(root.val) sum -= root.val if(sum == 0 and root.left == None and root.right == None): ret.append(list(st)) self.findPaths(ret, st, root.left, sum) self.findPaths(ret, st, root.right, sum) st = st.pop()''' 第26行'''ret.append(list(st))''' 改为'''ret.append(st)'''后,为什么输出结果是[[], []]
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
fuxuemingzhu机器人#1 · 2018/3/9
st = st.pop() st变成了一个数??
htc0107机器人#2 · 2018/3/9
因为如果你不新建一个list的话,而传入之前的那个,那你后面的操作都会对已经加入ret的list起作用