Skip to content

Commit e38737d

Browse files
committedApr 18, 2025
Add solution #1530
1 parent c84d83e commit e38737d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,349 LeetCode solutions in JavaScript
1+
# 1,350 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1169,6 +1169,7 @@
11691169
1526|[Minimum Number of Increments on Subarrays to Form a Target Array](./solutions/1526-minimum-number-of-increments-on-subarrays-to-form-a-target-array.js)|Hard|
11701170
1528|[Shuffle String](./solutions/1528-shuffle-string.js)|Easy|
11711171
1529|[Minimum Suffix Flips](./solutions/1529-minimum-suffix-flips.js)|Medium|
1172+
1530|[Number of Good Leaf Nodes Pairs](./solutions/1530-number-of-good-leaf-nodes-pairs.js)|Medium|
11721173
1534|[Count Good Triplets](./solutions/1534-count-good-triplets.js)|Easy|
11731174
1535|[Find the Winner of an Array Game](./solutions/1535-find-the-winner-of-an-array-game.js)|Medium|
11741175
1550|[Three Consecutive Odds](./solutions/1550-three-consecutive-odds.js)|Easy|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 1530. Number of Good Leaf Nodes Pairs
3+
* https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/
4+
* Difficulty: Medium
5+
*
6+
* You are given the root of a binary tree and an integer distance. A pair of two different leaf
7+
* nodes of a binary tree is said to be good if the length of the shortest path between them is
8+
* less than or equal to distance.
9+
*
10+
* Return the number of good leaf node pairs in the tree.
11+
*/
12+
13+
/**
14+
* Definition for a binary tree node.
15+
* function TreeNode(val, left, right) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.left = (left===undefined ? null : left)
18+
* this.right = (right===undefined ? null : right)
19+
* }
20+
*/
21+
/**
22+
* @param {TreeNode} root
23+
* @param {number} distance
24+
* @return {number}
25+
*/
26+
var countPairs = function(root, distance) {
27+
let result = 0;
28+
traverseTree(root);
29+
return result;
30+
31+
function traverseTree(node) {
32+
if (!node) return [];
33+
if (!node.left && !node.right) return [1];
34+
35+
const leftDistances = traverseTree(node.left);
36+
const rightDistances = traverseTree(node.right);
37+
38+
for (const left of leftDistances) {
39+
for (const right of rightDistances) {
40+
if (left + right <= distance) result++;
41+
}
42+
}
43+
44+
const allDistances = [];
45+
for (const dist of leftDistances.concat(rightDistances)) {
46+
if (dist + 1 <= distance) allDistances.push(dist + 1);
47+
}
48+
49+
return allDistances;
50+
}
51+
};

0 commit comments

Comments
 (0)
Please sign in to comment.