> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Good Leaf Nodes Pairs

> Tested Python solution for LeetCode 1530 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1530, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree), DP on Trees. [View on LeetCode](https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1530   # by problem number
lcpy gen -s number_of_good_leaf_nodes_pairs   # by problem name
```

## Problem

You are 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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/07/09/e1.jpg)

```
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](https://assets.leetcode.com/uploads/2020/07/09/e2.jpg)

```
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 the shortest path between them is 4.

```
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].

### Constraints

* The number of nodes in the tree is in the range \[1, 2^10].
* 1 \<= Node.val \<= 100
* 1 \<= distance \<= 10

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_good_leaf_nodes_pairs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_good_leaf_nodes_pairs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n * d^2) where d = distance (each leaf-depth list holds at most d entries)
    # Space: O(n)
    def count_pairs(self, root: TreeNode[int] | None, distance: int) -> int:
        total = 0

        def dfs(node: TreeNode[int] | None) -> list[int]:
            nonlocal total
            if node is None:
                return []
            if node.left is None and node.right is None:
                return [1]
            left = dfs(node.left)
            right = dfs(node.right)
            for left_depth in left:
                for right_depth in right:
                    if left_depth + right_depth <= distance:
                        total += 1
            return [depth + 1 for depth in left + right if depth + 1 < distance]

        dfs(root)
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n \* d^2) where d = distance (each leaf-depth list holds at most d entries) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.