-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path113.路径总和-ii.py
More file actions
34 lines (28 loc) · 883 Bytes
/
113.路径总和-ii.py
File metadata and controls
34 lines (28 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#
# @lc app=leetcode.cn id=113 lang=python3
#
# [113] 路径总和 II
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
path=[]
res=[]
def dfs(root,sum,path):
if not root:
return
if not root.left and not root.right:
if sum == root.val:
path=path+[root.val]
res.append(path)
dfs(root.left,sum-root.val,path+[root.val])
dfs(root.right,sum-root.val,path+[root.val])
dfs(root,targetSum,path)
return res
# @lc code=end