my python solution


  • 0
    A
    class Solution(object):
        def pathSum(self, root, sum):
            result = []
            if root == None:
                return result
                
            self.helper(root, sum, [], result)
            
            return result
            
        def helper(self, root, sum, path, result):
            if root == None:
                return
            
            path.append(root.val)
            
            if root.left == None and root.right == None:
                if root.val == sum:
                    # make new list instead of result.append(path) which will reference path.
                    result.append([] + path)
            else:
                self.helper(root.left, sum - root.val, path, result)
                self.helper(root.right, sum - root.val, path, result)
                
            path.pop()
    

Log in to reply
 

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