Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Good Leaf Nodes Pairs

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1530" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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 = [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 tree is in the range [1, 210]. 1 <= Node.val <= 100 1 <= distance <= 10

Explanation

Here's a solution to the problem of counting good leaf node pairs in a binary tree within a specified distance.

  • High-Level Approach:

    • Perform a post-order traversal of the tree.
    • For each node, calculate the distances to all leaf nodes in its subtrees. Store these distances in a list.
    • When returning from a node, count the number of good leaf pairs between its left and right subtrees based on the given distance.
  • Complexity:

    • Runtime Complexity: O(N * distance), where N is the number of nodes in the tree.
    • Storage Complexity: O(N), due to recursion stack and storing leaf distances.

Code

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

def countGoodLeafNodesPairs(root, distance):
    count = 0

    def dfs(node):
        nonlocal count
        if not node:
            return []

        if not node.left and not node.right:
            return [0]

        left_distances = dfs(node.left)
        right_distances = dfs(node.right)

        for l_dist in left_distances:
            for r_dist in right_distances:
                if l_dist + r_dist + 2 <= distance:
                    count += 1

        updated_distances = []
        for dist in left_distances:
            if dist + 1 < distance:
                updated_distances.append(dist + 1)
        for dist in right_distances:
            if dist + 1 < distance:
                updated_distances.append(dist + 1)

        return updated_distances

    dfs(root)
    return count

More from this blog

C

Chatmagic blog

2894 posts