"""
Given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to distance.
Return the number of good leaf node pairs in the tree.

Example 1:
Input: root = [1,2,3,null,4], distance = 3
Output: 1
Explanation: The leaf nodes of the tree are 3 and 4 and the length of the shortest path between them is 3. This is the only good pair.

Example 2:
Input: root = [1,2,3,4,5,6,7], distance = 3
Output: 2
Explanation: The good pairs are [4,5] and [6,7] with shortest path = 2. The pair [4,6] is not good because the length of ther shortest path between them is 4.

Example 3:
Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
Output: 1
Explanation: The only good pair is [2,5].

Example 4:
Input: root = [100], distance = 1
Output: 0

Example 5:
Input: root = [1,1,1], distance = 2
Output: 1

Constraints:
The number of nodes in the tree is in the range [1, 2^10].
Each node's value is between [1, 100].
1 <= distance <= 10
"""


# 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 countPairs(self, root: TreeNode, distance: int) -> int:
        res = [0]
        self.dfs(root, distance, res)
        
        return res[0]
        
    def dfs(self, root: TreeNode, distance: int, res: List[int]) -> None:
        if not root:
            return []
        
        if not root.left and not root.right:
            return [1]
        
        left = self.dfs(root.left, distance, res)
        right = self.dfs(root.right, distance, res)
        res[0] += sum(l + r <= distance for l in left for r in right)
        
        return [n+1 for n in left + right if n+1 < distance]
        
e99b1ae97b9834288b6606fa874e45850187c9e4
<mask token>


class Solution:

    def countPairs(self, root: TreeNode, distance: int) ->int:
        res = [0]
        self.dfs(root, distance, res)
        return res[0]
    <mask token>

<mask token>


class Solution:

    def countPairs(self, root: TreeNode, distance: int) ->int:
        res = [0]
        self.dfs(root, distance, res)
        return res[0]

    def dfs(self, root: TreeNode, distance: int, res: List[int]) ->None:
        if not root:
            return []
        if not root.left and not root.right:
            return [1]
        left = self.dfs(root.left, distance, res)
        right = self.dfs(root.right, distance, res)
        res[0] += sum(l + r <= distance for l in left for r in right)
        return [(n + 1) for n in left + right if n + 1 < distance]

<mask token>


class TreeNode:
    <mask token>


class Solution:

    def countPairs(self, root: TreeNode, distance: int) ->int:
        res = [0]
        self.dfs(root, distance, res)
        return res[0]

    def dfs(self, root: TreeNode, distance: int, res: List[int]) ->None:
        if not root:
            return []
        if not root.left and not root.right:
            return [1]
        left = self.dfs(root.left, distance, res)
        right = self.dfs(root.right, distance, res)
        res[0] += sum(l + r <= distance for l in left for r in right)
        return [(n + 1) for n in left + right if n + 1 < distance]

<mask token>


class TreeNode:

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


class Solution:

    def countPairs(self, root: TreeNode, distance: int) ->int:
        res = [0]
        self.dfs(root, distance, res)
        return res[0]

    def dfs(self, root: TreeNode, distance: int, res: List[int]) ->None:
        if not root:
            return []
        if not root.left and not root.right:
            return [1]
        left = self.dfs(root.left, distance, res)
        right = self.dfs(root.right, distance, res)
        res[0] += sum(l + r <= distance for l in left for r in right)
        return [(n + 1) for n in left + right if n + 1 < distance]

"""
Given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to distance.
Return the number of good leaf node pairs in the tree.

Example 1:
Input: root = [1,2,3,null,4], distance = 3
Output: 1
Explanation: The leaf nodes of the tree are 3 and 4 and the length of the shortest path between them is 3. This is the only good pair.

Example 2:
Input: root = [1,2,3,4,5,6,7], distance = 3
Output: 2
Explanation: The good pairs are [4,5] and [6,7] with shortest path = 2. The pair [4,6] is not good because the length of ther shortest path between them is 4.

Example 3:
Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
Output: 1
Explanation: The only good pair is [2,5].

Example 4:
Input: root = [100], distance = 1
Output: 0

Example 5:
Input: root = [1,1,1], distance = 2
Output: 1

Constraints:
The number of nodes in the tree is in the range [1, 2^10].
Each node's value is between [1, 100].
1 <= distance <= 10
"""


# 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 countPairs(self, root: TreeNode, distance: int) -> int:
        res = [0]
        self.dfs(root, distance, res)
        
        return res[0]
        
    def dfs(self, root: TreeNode, distance: int, res: List[int]) -> None:
        if not root:
            return []
        
        if not root.left and not root.right:
            return [1]
        
        left = self.dfs(root.left, distance, res)
        right = self.dfs(root.right, distance, res)
        res[0] += sum(l + r <= distance for l in left for r in right)
        
        return [n+1 for n in left + right if n+1 < distance]
        