Don't know whats wrong with code. Halp!


  • 0
    P

    For input: [1] 1, my output is [[5,4,11,2],[5,8,4,5],[1]] when the expected output is [[1]]. I can't figure out why my output even has those first 2 terms when they're not part of the input! Please help me sort out this bizarre bug. Thanks!

    class Solution(object):
        ret=[]
        def pathSum(self, root, target):
            """
            :type root: TreeNode
            :type sum: int
            :rtype: List[List[int]]
            """
            if root==None:
                return []
            
            def path(root, pathSoFar, target):
                if root==None:
                    return
                if root.left==None and root.right==None:
                    listi=pathSoFar.strip().split()
                    listi=map(int, listi)
                    listi.append(root.val)
                    if sum(listi)==target:
                        self.ret.append(listi)
                path(root.left, pathSoFar + " " + str(root.val), target)
                path(root.right, pathSoFar + " " + str(root.val), target)
            path(root, "", target)
            a=self.ret
            self.ret=[]
            return a
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.