Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e99375e

Browse files
Daily Solutions With JS
1 parent 898df56 commit e99375e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var countPairs = function(root, distance) {
2+
let output = 0;
3+
4+
function callDFS(node) {
5+
if(!node) return null;
6+
if(!node.left && !node.right) return [1];
7+
8+
const left = callDFS(node.left);
9+
const right = callDFS(node.right);
10+
11+
if(!node.left || !node.right) {
12+
if(left) return left.map(v => v + 1);
13+
return right.map(v => v + 1)
14+
}
15+
16+
for(let l of left) {
17+
for(let r of right) {
18+
if(l + r <= distance) output++;
19+
}
20+
}
21+
return [...left, ...right].map(v => v + 1);
22+
}
23+
callDFS(root);
24+
return output;
25+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /