Path Sum II, Time Limit Exceeded ?


  • 0
    A

    This is my code. I am so confused about why it give rise to time limit exceeded ?
    Can anyone help me ? I have thought about it the whole day.

    class Solution(object):
        def pathSum(self, root, sum):
            """
            :type root: TreeNode
            :type sum: int
            :rtype: List[List[int]]
            """
            total = []
            
            if root==None:
                return total 
            
            return self.helper(sum, root, [root.val], root.val, total)
        
        
        
        def helper(self, sum, root, curlist, curvalue, total):
            if root.left==None and root.right==None:
                if curvalue==sum:
                    total.append(curlist)
                    return total 
            
            if root.left!=None:
                curlist.append(root.left.val)
                total = self.helper(sum, root.left, curlist, curvalue + root.left.val, total)
            if root.right!=None:
                curlist.append(root.right.val)
                total = self.helper(sum, root.right, curlist, curvalue + root.right.val, total)
           
            return total

  • 0
    H

    Hi ayst, I have the same problem with you. Have you solved it?


Log in to reply
 

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