1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# @Title: 二叉树中和为某一值的路径 (二叉树中和为某一值的路径 LCOF)
# @Author: 15816537946@163.com
# @Date: 2022-02-17 00:52:36
# @Runtime: 44 ms
# @Memory: 16.5 MB
class Solution:
    def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
        res, path = [], []
        def recur(root, tar):
            if not root: return
            path.append(root.val)
            tar -= root.val
            if tar == 0 and not root.left and not root.right:
                res.append(list(path))
            recur(root.left, tar)
            recur(root.right, tar)
            path.pop()

        recur(root, sum)
        return res