-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathSumII.js
39 lines (35 loc) · 1.09 KB
/
PathSumII.js
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
//Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references.
//Runtime Complexity
//Polynomial, O(n^2).
//Memory Complexity
//Polynomial, O(n^2).
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} targetSum
* @return {number[][]}
*/
var pathSum = function(root, targetSum) {
let result = [];
const dfs = (root, sum, path) => {
if (root) {
path.push(root.val);
sum += root.val;
if (!root.left && !root.right && sum === targetSum) {
result.push([...path])
}
dfs(root.left, sum, path)
dfs(root.right, sum, path)
path.pop();
}
}
dfs(root, 0, []);
return result;
};