Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1530: Number of Good Leaf Nodes Pairs

In this guide, we solve Leetcode #1530 Number of Good Leaf Nodes Pairs in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Tree, Depth-First Search, Binary Tree

Intuition

We need to explore a structure deeply before backing up, which suits DFS.

DFS keeps local context on the call stack and is easy to implement recursively.

Approach

Define a recursive DFS that carries the necessary state.

Combine child results as the recursion unwinds.

Steps:

  • Define a recursive DFS with state.
  • Visit children and combine results.
  • Return the final aggregation.

Example

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.

Python Solution

# 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: def dfs(root, cnt, i): if root is None or i >= distance: return if root.left is None and root.right is None: cnt[i] += 1 return dfs(root.left, cnt, i + 1) dfs(root.right, cnt, i + 1) if root is None: return 0 ans = self.countPairs(root.left, distance) + self.countPairs( root.right, distance ) cnt1 = Counter() cnt2 = Counter() dfs(root.left, cnt1, 1) dfs(root.right, cnt2, 1) for k1, v1 in cnt1.items(): for k2, v2 in cnt2.items(): if k1 + k2 <= distance: ans += v1 * v2 return ans

Complexity

The time complexity is O(n×d2×h)O(n \times d^2 \times h)O(n×d2×h), where nnn is the number of nodes in the binary tree, and hhh and ddd are the height of the binary tree and the distance limit, respectively. The space complexity is O(h)O(h)O(h) for the recursion stack.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy