-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path113. Path Sum II.ts
60 lines (52 loc) · 1.35 KB
/
113. Path Sum II.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{
/**
* Runtime 146 ms Beats 39.19%
* Memory 61.5 MB Beats 12.16%
*/
/**
* Definition for a binary tree node.
*/
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}
const pathSum = (root: TreeNode | null, targetSum: number): number[][] => {
if (!root) {
return [];
}
const paths: number[][] = [];
const dfs = (node: TreeNode | null, sum: number, path: number[]) => {
if (!node) {
return;
}
if (!node.left && !node.right && sum + node.val === targetSum) {
paths.push([...path, node.val]);
return;
}
dfs(node.left, sum + node.val, [...path, node.val]);
dfs(node.right, sum + node.val, [...path, node.val]);
};
dfs(root, 0, []);
return paths;
};
console.log(
pathSum(
new TreeNode(
5,
new TreeNode(4, new TreeNode(11, new TreeNode(7), new TreeNode(2))),
new TreeNode(
8,
new TreeNode(13),
new TreeNode(4, new TreeNode(5), new TreeNode(1))
)
),
22
)
); // [[5, 4, 11, 2], [5, 8, 4, 5]]
}