LeetCode 1530: Number of Good Leaf Nodes Pairs Solution
Master LeetCode problem 1530 (Number of Good Leaf Nodes Pairs), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1530. Number of Good Leaf Nodes Pairs
Problem Explanation
Explanation
To solve this problem, we can perform a depth-first search (DFS) starting from each leaf node to find all possible paths to other leaf nodes. For each path, we calculate the distance between the two leaf nodes and increment the count if the distance is less than or equal to the given distance. We can keep track of the distances between each leaf node and their ancestors using a hash map. The time complexity of this approach is O(n^2) where n is the number of nodes in the tree.
Solution Code
class Solution {
public int countPairs(TreeNode root, int distance) {
Map<TreeNode, Integer> leafDistances = new HashMap<>();
return dfs(root, distance, leafDistances);
}
private int dfs(TreeNode node, int distance, Map<TreeNode, Integer> leafDistances) {
if (node == null) {
return 0;
}
if (node.left == null && node.right == null) {
Map<Integer, Integer> pathCounts = new HashMap<>();
pathCounts.put(0, 1);
return findPairs(node, distance, pathCounts, leafDistances);
}
int leftCount = dfs(node.left, distance, leafDistances);
int rightCount = dfs(node.right, distance, leafDistances);
return leftCount + rightCount;
}
private int findPairs(TreeNode node, int distance, Map<Integer, Integer> pathCounts, Map<TreeNode, Integer> leafDistances) {
int pairs = 0;
for (Map.Entry<Integer, Integer> entry : pathCounts.entrySet()) {
int totalDistance = entry.getKey();
int count = entry.getValue();
leafDistances.put(node, totalDistance);
if (node.left == null && node.right == null) {
pairs += count;
} else {
if (node.left != null) {
Map<Integer, Integer> leftPathCounts = new HashMap<>();
updatePathCounts(node.left, totalDistance + 1, leftPathCounts, leafDistances);
pairs += findPairs(node.left, distance, leftPathCounts, leafDistances);
}
if (node.right != null) {
Map<Integer, Integer> rightPathCounts = new HashMap<>();
updatePathCounts(node.right, totalDistance + 1, rightPathCounts, leafDistances);
pairs += findPairs(node.right, distance, rightPathCounts, leafDistances);
}
}
}
return pairs;
}
private void updatePathCounts(TreeNode node, int distance, Map<Integer, Integer> pathCounts, Map<TreeNode, Integer> leafDistances) {
if (leafDistances.containsKey(node)) {
int currentDistance = leafDistances.get(node);
int newDistance = currentDistance + distance;
pathCounts.put(newDistance, pathCounts.getOrDefault(newDistance, 0) + 1);
} else {
Map<Integer, Integer> newPaths = new HashMap<>();
newPaths.put(distance, 1);
leafDistances.put(node, distance);
for (Map.Entry<Integer, Integer> entry : pathCounts.entrySet()) {
int totalDistance = entry.getKey();
int count = entry.getValue();
int newDistance = totalDistance + distance;
newPaths.put(newDistance, newPaths.getOrDefault(newDistance, 0) + count);
}
pathCounts.clear();
pathCounts.putAll(newPaths);
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1530 (Number of Good Leaf Nodes Pairs)?
This page provides optimized solutions for LeetCode problem 1530 (Number of Good Leaf Nodes Pairs) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1530 (Number of Good Leaf Nodes Pairs)?
The time complexity for LeetCode 1530 (Number of Good Leaf Nodes Pairs) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1530 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1530 in Java, C++, or Python.