Skip to content

Commit ec22bab

Browse files
authored
feat: add js solution to lc problem: No.1530 (#3289)
1 parent 0caf311 commit ec22bab

File tree

4 files changed

+251
-0
lines changed

4 files changed

+251
-0
lines changed

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,93 @@ func dfs(root *TreeNode, cnt []int, i int) {
270270
}
271271
```
272272

273+
#### TypeScript
274+
275+
```ts
276+
function countPairs(root: TreeNode | null, distance: number): number {
277+
const pairs: number[][] = [];
278+
279+
const dfs = (node: TreeNode | null): number[][] => {
280+
if (!node) return [];
281+
if (!node.left && !node.right) return [[node.val, 1]];
282+
283+
const left = dfs(node.left);
284+
const right = dfs(node.right);
285+
286+
for (const [x, dx] of left) {
287+
for (const [y, dy] of right) {
288+
if (dx + dy <= distance) {
289+
pairs.push([x, y]);
290+
}
291+
}
292+
}
293+
294+
const res: number[][] = [];
295+
for (const arr of [left, right]) {
296+
for (const x of arr) {
297+
if (++x[1] <= distance) res.push(x);
298+
}
299+
}
300+
301+
return res;
302+
};
303+
304+
dfs(root);
305+
306+
return pairs.length;
307+
}
308+
```
309+
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+
273360
<!-- tabs:end -->
274361

275362
<!-- solution:end -->

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,93 @@ func dfs(root *TreeNode, cnt []int, i int) {
245245
}
246246
```
247247

248+
#### TypeScript
249+
250+
```ts
251+
function countPairs(root: TreeNode | null, distance: number): number {
252+
const pairs: number[][] = [];
253+
254+
const dfs = (node: TreeNode | null): number[][] => {
255+
if (!node) return [];
256+
if (!node.left && !node.right) return [[node.val, 1]];
257+
258+
const left = dfs(node.left);
259+
const right = dfs(node.right);
260+
261+
for (const [x, dx] of left) {
262+
for (const [y, dy] of right) {
263+
if (dx + dy <= distance) {
264+
pairs.push([x, y]);
265+
}
266+
}
267+
}
268+
269+
const res: number[][] = [];
270+
for (const arr of [left, right]) {
271+
for (const x of arr) {
272+
if (++x[1] <= distance) res.push(x);
273+
}
274+
}
275+
276+
return res;
277+
};
278+
279+
dfs(root);
280+
281+
return pairs.length;
282+
}
283+
```
284+
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+
248335
<!-- tabs:end -->
249336

250337
<!-- 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+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function countPairs(root: TreeNode | null, distance: number): number {
2+
const pairs: number[][] = [];
3+
4+
const dfs = (node: TreeNode | null): number[][] => {
5+
if (!node) return [];
6+
if (!node.left && !node.right) return [[node.val, 1]];
7+
8+
const left = dfs(node.left);
9+
const right = dfs(node.right);
10+
11+
for (const [x, dx] of left) {
12+
for (const [y, dy] of right) {
13+
if (dx + dy <= distance) {
14+
pairs.push([x, y]);
15+
}
16+
}
17+
}
18+
19+
const res: number[][] = [];
20+
for (const arr of [left, right]) {
21+
for (const x of arr) {
22+
if (++x[1] <= distance) res.push(x);
23+
}
24+
}
25+
26+
return res;
27+
};
28+
29+
dfs(root);
30+
31+
return pairs.length;
32+
}

0 commit comments

Comments
 (0)