Python recursive solution, a question exist.


  • 0
    K

    Spoilerclass Solution(object):

    def recursive_dfs(self,root,sum,paths,path,path_sum):
    
    	if root:
    
    		path.append(root.val)
    		path_sum += root.val
    
    		if not root.left and not root.right:
    			if path_sum == sum:
    				paths.append(list(path))#??
    
    		self.recursive_dfs(root.left,sum,paths,path,path_sum)
    		self.recursive_dfs(root.right,sum,paths,path,path_sum)
    		path.pop()
    		
    def pathSum(self, root, sum):
        
        paths = []
        path = []
        self.recursive_dfs(root,sum,paths,path,0)
        return paths

  • 0
    K

    Anyone knows why replacing line14 with:

    paths.append(path)

    namely, removing the list() method gives null arrays?


Log in to reply
 

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