Skip to content

Commit 442b1a2

Browse files
committed
feat: add js solution to lc problem: No.1530
1 parent 8e82b98 commit 442b1a2

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

solution/1500-1599/1530.Number of Good Leaf Nodes Pairs/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,56 @@ function countPairs(root: TreeNode | null, distance: number): number {
307307
}
308308
```
309309

310+
#### JavaScript
311+
312+
```js
313+
/**
314+
* Definition for a binary tree node.
315+
* function TreeNode(val, left, right) {
316+
* this.val = (val===undefined ? 0 : val)
317+
* this.left = (left===undefined ? null : left)
318+
* this.right = (right===undefined ? null : right)
319+
* }
320+
*/
321+
/**
322+
* @param {TreeNode} root
323+
* @param {number} distance
324+
* @return {number}
325+
*/
326+
var countPairs = function (root, distance) {
327+
const pairs = [];
328+
329+
const dfs = node => {
330+
if (!node) return [];
331+
if (!node.left && !node.right) return [[node.val, 1]];
332+
333+
const left = dfs(node.left);
334+
const right = dfs(node.right);
335+
336+
for (const [x, dx] of left) {
337+
for (const [y, dy] of right) {
338+
if (dx + dy <= distance) {
339+
pairs.push([x, y]);
340+
}
341+
}
342+
}
343+
344+
const res = [];
345+
for (const arr of [left, right]) {
346+
for (const x of arr) {
347+
if (++x[1] <= distance) res.push(x);
348+
}
349+
}
350+
351+
return res;
352+
};
353+
354+
dfs(root);
355+
356+
return pairs.length;
357+
};
358+
```
359+
310360
<!-- tabs:end -->
311361

312362
<!-- solution:end -->

solution/1500-1599/1530.Number of Good Leaf Nodes Pairs/README_EN.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,56 @@ function countPairs(root: TreeNode | null, distance: number): number {
282282
}
283283
```
284284

285+
#### JavaScript
286+
287+
```js
288+
/**
289+
* Definition for a binary tree node.
290+
* function TreeNode(val, left, right) {
291+
* this.val = (val===undefined ? 0 : val)
292+
* this.left = (left===undefined ? null : left)
293+
* this.right = (right===undefined ? null : right)
294+
* }
295+
*/
296+
/**
297+
* @param {TreeNode} root
298+
* @param {number} distance
299+
* @return {number}
300+
*/
301+
var countPairs = function (root, distance) {
302+
const pairs = [];
303+
304+
const dfs = node => {
305+
if (!node) return [];
306+
if (!node.left && !node.right) return [[node.val, 1]];
307+
308+
const left = dfs(node.left);
309+
const right = dfs(node.right);
310+
311+
for (const [x, dx] of left) {
312+
for (const [y, dy] of right) {
313+
if (dx + dy <= distance) {
314+
pairs.push([x, y]);
315+
}
316+
}
317+
}
318+
319+
const res = [];
320+
for (const arr of [left, right]) {
321+
for (const x of arr) {
322+
if (++x[1] <= distance) res.push(x);
323+
}
324+
}
325+
326+
return res;
327+
};
328+
329+
dfs(root);
330+
331+
return pairs.length;
332+
};
333+
```
334+
285335
<!-- tabs:end -->
286336

287337
<!-- solution:end -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number} distance
12+
* @return {number}
13+
*/
14+
var countPairs = function (root, distance) {
15+
const pairs = [];
16+
17+
const dfs = node => {
18+
if (!node) return [];
19+
if (!node.left && !node.right) return [[node.val, 1]];
20+
21+
const left = dfs(node.left);
22+
const right = dfs(node.right);
23+
24+
for (const [x, dx] of left) {
25+
for (const [y, dy] of right) {
26+
if (dx + dy <= distance) {
27+
pairs.push([x, y]);
28+
}
29+
}
30+
}
31+
32+
const res = [];
33+
for (const arr of [left, right]) {
34+
for (const x of arr) {
35+
if (++x[1] <= distance) res.push(x);
36+
}
37+
}
38+
39+
return res;
40+
};
41+
42+
dfs(root);
43+
44+
return pairs.length;
45+
};

0 commit comments

Comments
 (0)