#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
给定一个二叉树和一个目标和，找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

说明: 叶子节点是指没有子节点的节点。

示例:
给定如下二叉树，以及目标和 sum = 22，

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1
返回:

[
   [5,4,11,2],
   [5,8,4,5]
]

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/path-sum-ii
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def __init__(self):
        self.res = []

    def pathSum(self, root: TreeNode, sum: int) -> list[list[int]]:
        self.total = sum
        self.dfs(root, [])
        return self.res

    def dfs(self, root, path):
        if not root:
            return

        path.append(root.val)
        if root.left is None and root.right is None:
            if sum(path) == self.total:
                self.res.append(path)
        self.dfs(root.left, path[:])
        self.dfs(root.right, path[:])

821d7cd40aae417a62cab00ac29be73b5af33698
<mask token>


class Solution:

    def __init__(self):
        self.res = []

    def pathSum(self, root: TreeNode, sum: int) ->list[list[int]]:
        self.total = sum
        self.dfs(root, [])
        return self.res
    <mask token>

<mask token>


class Solution:

    def __init__(self):
        self.res = []

    def pathSum(self, root: TreeNode, sum: int) ->list[list[int]]:
        self.total = sum
        self.dfs(root, [])
        return self.res

    def dfs(self, root, path):
        if not root:
            return
        path.append(root.val)
        if root.left is None and root.right is None:
            if sum(path) == self.total:
                self.res.append(path)
        self.dfs(root.left, path[:])
        self.dfs(root.right, path[:])

<mask token>


class TreeNode:
    <mask token>


class Solution:

    def __init__(self):
        self.res = []

    def pathSum(self, root: TreeNode, sum: int) ->list[list[int]]:
        self.total = sum
        self.dfs(root, [])
        return self.res

    def dfs(self, root, path):
        if not root:
            return
        path.append(root.val)
        if root.left is None and root.right is None:
            if sum(path) == self.total:
                self.res.append(path)
        self.dfs(root.left, path[:])
        self.dfs(root.right, path[:])

<mask token>


class TreeNode:

    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:

    def __init__(self):
        self.res = []

    def pathSum(self, root: TreeNode, sum: int) ->list[list[int]]:
        self.total = sum
        self.dfs(root, [])
        return self.res

    def dfs(self, root, path):
        if not root:
            return
        path.append(root.val)
        if root.left is None and root.right is None:
            if sum(path) == self.total:
                self.res.append(path)
        self.dfs(root.left, path[:])
        self.dfs(root.right, path[:])

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
给定一个二叉树和一个目标和，找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

说明: 叶子节点是指没有子节点的节点。

示例:
给定如下二叉树，以及目标和 sum = 22，

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1
返回:

[
   [5,4,11,2],
   [5,8,4,5]
]

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/path-sum-ii
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def __init__(self):
        self.res = []

    def pathSum(self, root: TreeNode, sum: int) -> list[list[int]]:
        self.total = sum
        self.dfs(root, [])
        return self.res

    def dfs(self, root, path):
        if not root:
            return

        path.append(root.val)
        if root.left is None and root.right is None:
            if sum(path) == self.total:
                self.res.append(path)
        self.dfs(root.left, path[:])
        self.dfs(root.right, path[:])
