Easy Python code using tuple stack code


  • 0
    W
    class Solution(object):
        def pathSum(self, root, sum1):
            """
            :type root: TreeNode
            :type sum: int
            :rtype: List[List[int]]
            """
            if root==None:
                return []
            stack,res=[(root,[root.val])],[]
            while stack:
                temp,val=stack.pop()
                if not temp.left and not temp.right:
                    if sum(val)==sum1:
                        res.append(val)
                if temp.left:
                    stack.append((temp.left,val+[temp.left.val]))
                if temp.right:
                    stack.append((temp.right,val+[temp.right.val]))
            return res

Log in to reply
 

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