Number of Good Leaf Nodes Pairs Problem
Description
LeetCode Problem 1530.
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.
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].
Constraints:
- The number of nodes in the
treeis in the range[1, 2^10]. 1 <= Node.val <= 1001 <= distance <= 10
Solution
Approach: Post-Order DFS with Leaf Depth Counts
Two leaves are joined by a path that goes up to their lowest common ancestor and back down. So every pair can be counted exactly once, at the node where the two leaves split: one leaf in its left subtree, the other in its right subtree.
Let dfs(node) return a small array cnt, where cnt[d] is the number of leaves exactly d edges below node. Only depths up to distance matter, because a deeper leaf is already too far from everything outside its subtree.
- An empty subtree returns all zeros, and a leaf returns
cnt[0] = 1. - For any other node, take
left = dfs(node.left)andright = dfs(node.right). A leaf at depthibelow the left child and a leaf at depthjbelow the right child are(i + 1) + (j + 1)edges apart, so addleft[i] * right[j]for everyi + j + 2 <= distance. - Then pass the counts up one level:
cnt[d + 1] = left[d] + right[d], dropping whatever would go pastdistance.
Pairs inside one subtree were already counted lower down, so nothing is counted twice. The tricky case is a node with one child: it is not a leaf, and the missing side simply contributes zeros.
Complexity: O(n * distance2) time and O(h * distance) extra space for the recursion, where n is the number of nodes and h the height of the tree. With distance <= 10 this is linear in practice.
# 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: Optional[TreeNode], distance: int) -> int:
pairs = 0
def dfs(node):
# cnt[d] = number of leaves exactly d edges below node (deeper ones never matter)
nonlocal pairs
cnt = [0] * (distance + 1)
if node is None:
return cnt
if node.left is None and node.right is None:
cnt[0] = 1
return cnt
left, right = dfs(node.left), dfs(node.right)
# A left leaf at depth i and a right leaf at depth j are (i + 1) + (j + 1) apart.
for i in range(distance):
for j in range(distance - 1 - i):
pairs += left[i] * right[j]
for d in range(distance):
cnt[d + 1] = left[d] + right[d]
return cnt
dfs(root)
return pairs