-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113.js
More file actions
39 lines (38 loc) · 875 Bytes
/
113.js
File metadata and controls
39 lines (38 loc) · 875 Bytes
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
/**
* 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 res = []
let arr = []
const bt = (root, target) => {
if (root === null) return
arr.push(root.val)
if (!(root.left || root.right)) {
if (root.val === target) {
res.push([...arr])
}
} else {
bt(root.left, target - root.val)
bt(root.right, target - root.val)
}
arr.pop()
return
}
bt(root, targetSum)
return res
}
/*
2021/9/28
42 58
回溯
*/