-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumII.py
More file actions
24 lines (22 loc) · 799 Bytes
/
PathSumII.py
File metadata and controls
24 lines (22 loc) · 799 Bytes
1
# Definition for a binary tree node# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param root, a tree node # @param sum, an integer # @return a list of lists of integers def pathSum(self, root, sum): def dfs(root, currsum, valuelist): if root.left==None and root.right==None: if currsum==sum: res.append(valuelist) if root.left: dfs(root.left, currsum+root.left.val, valuelist+[root.left.val]) if root.right: dfs(root.right, currsum+root.right.val, valuelist+[root.right.val]) res=[] if root==None: return [] dfs(root, root.val, [root.val]) return res