1530. Number of Good Leaf Nodes Pairs (18 July 2024)
Introduction
Today's challenge, "Number of Good Leaf Nodes Pairs," asks us to count all pairs of leaf nodes in a binary tree that are within a specified distance d. This problem is a great exercise in depth-first search (DFS) and understanding the tree data structure dynamics.
Problem Statement
Given a binary tree with n nodes, where each node has a distinct value from 1 to n, and a number distance. A pair of leaf nodes (leaf nodes are nodes with no children) is said to be good if the length of the shortest path between them is less than or equal to distance. We need to return the number of good leaf node pairs in the tree.
Solution Strategy
The idea is to use a recursive depth-first search (DFS) approach to explore each node. We aim to:
- Identify the leaf nodes.
- Calculate the distance from each leaf node to its ancestors.
- Determine the number of good pairs for each node by comparing the distances of leaf nodes under its left and right subtree.
Here's the breakdown of the steps involved:
Step 1: Leaf Identification
For each node, if both left and right child nodes are nullptr, it is a leaf node.
Step 2: Accumulating Distances
For each leaf node, we track its distance from the current node in a vector. This helps in calculating the number of good pairs effectively.
Step 3: Counting Good Pairs
For each node, we compare every pair of distances (one from the left subtree and one from the right subtree). If their sum plus two (to account for the edges to the current node) is less than or equal to distance, it's a good pair.
Step 4: Propagate Distances
Propagate the distances from leaf nodes to their parent, incrementing each by one, unless adding one exceeds the specified distance.
Implementation
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int countPairs(TreeNode* root, int distance) {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int count = 0;
helper(root, distance, count);
return count;
}
private:
vector<int> helper(TreeNode* root, int distance, int &count) {
if (!root) return {};
if (!root->left && !root->right) {
return {0}; // Leaf node: return a vector with only one element, the distance 0.
}
vector<int> leftDistances = helper(root->left, distance, count);
vector<int> rightDistances = helper(root->right, distance, count);
for (int ld : leftDistances) {
for (int rd : rightDistances) {
if (ld + rd + 2 <= distance) {
count++; // Count pairs where the sum of distances is <= distance.
}
}
}
vector<int> result;
for (int ld : leftDistances) {
if (ld + 1 < distance) {
result.push_back(ld + 1);
}
}
for (int rd : rightDistances) {
if (rd + 1 < distance) {
result.push_back(rd + 1);
}
}
return result;
}
};