Advertisement

Number of Good Leaf Nodes Pairs - LeetCode 1530 Solution

Number of Good Leaf Nodes Pairs - Complete Solution Guide

Number of Good Leaf Nodes Pairs is LeetCode problem 1530, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 . 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 =

Detailed Explanation

The problem asks us to find the number of 'good' leaf node pairs in a binary tree. A 'good' pair is defined as two distinct leaf nodes whose shortest path length is less than or equal to a given 'distance'. The input is the root of the binary tree and the integer 'distance'. The output is the count of good leaf node pairs.

Solution Approach

The solution employs a recursive Depth-First Search (DFS) approach. The `dfs` function returns a list of distances from leaf nodes in the subtree rooted at the current node to the current node. For each node, it recursively calls `dfs` on its left and right children. If the current node is a leaf node, it returns a list containing only 0 (since the distance from a leaf to itself is 0). If the current node is not a leaf node, it gets the lists of distances from its left and right children. It then increments each distance in these lists (to account for the edge connecting the child to the current node). Finally, it iterates through all possible pairs of distances (one from the left subtree and one from the right subtree) and increments the global `count` if their sum is less than or equal to `distance`. The function returns a list of distances from descendant leaf nodes to the current node, excluding distances which are already greater than or equal to 'distance'.

Step-by-Step Algorithm

  1. Step 1: Initialize a global counter `count` to 0. This counter will store the number of good leaf node pairs.
  2. Step 2: Define a recursive function `dfs(node, distance)` that takes a node and the maximum allowed distance as input.
  3. Step 3: Base Case: If the node is null, return an empty list. If the node is a leaf node (no left and right children), return a list containing only 0 (distance from itself).
  4. Step 4: Recursively call `dfs` on the left and right children of the current node to obtain lists of distances from leaf nodes in those subtrees to their respective children.
  5. Step 5: Increment each distance in the lists returned from the left and right children by 1 to represent the distance from the leaf nodes to the current node.
  6. Step 6: Iterate through all possible pairs of distances (one from the left subtree and one from the right subtree). If the sum of any pair of distances is less than or equal to the given `distance`, increment the global `count`.
  7. Step 7: Combine the distances from the left and right subtrees into a single list. Filter this list to keep only distances that are strictly less than the given `distance`.
  8. Step 8: Return the filtered list of distances from the current node to its descendant leaf nodes.
  9. Step 9: Call the `dfs` function with the root of the tree and the given `distance`.
  10. Step 10: Return the final value of the `count`.

Key Insights

  • Insight 1: We need to use Depth-First Search (DFS) to traverse the tree and find all leaf nodes.
  • Insight 2: For each node, we need to keep track of the distances from its descendant leaf nodes to itself.
  • Insight 3: When we reach a node, we can combine the distances from leaf nodes in its left and right subtrees to identify good pairs.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n)

Topics

This problem involves: Tree, Depth-First Search, Binary Tree.

Companies

Asked at: ByteDance, josh technology.