-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path0113-path-sum-ii.js
43 lines (38 loc) · 1.25 KB
/
0113-path-sum-ii.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
40
41
42
43
/**
* 113. Path Sum II
* https://leetcode.com/problems/path-sum-ii/
* Difficulty: Medium
*
* 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.
*
* A root-to-leaf path is a path starting from the root and ending at any leaf node. A leaf is a
* node with no children.
*/
/**
* 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) {
if (!root) return [];
return traverse([], targetSum, root);
};
function traverse(result, targetSum, node, history = []) {
const values = [...history, node.val];
if (!node.left && !node.right && values.reduce((sum, n) => sum + n, 0) === targetSum) {
result.push(values);
}
if (node.left) traverse(result, targetSum, node.left, values);
if (node.right) traverse(result, targetSum, node.right, values);
return result;
}