一、题目地址
https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/
二、具体代码
/**
* 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} target
* @return {number[][]}
*/
// 时间复杂度:O(N)
// 空间复杂度:O(N)
var pathSum = function(root, target) {
let res = [];
let path = [];
recur(root, target, res, path);
return res;
};
function recur(root, target, res, path) {
if(root === null) {
return;
}
path.push(root.val);
target -= root.val;
if(target === 0 && root.left === null && root.right === null) {
res.push(path.slice(0));
}
recur(root.left, target, res, path);
recur(root.right, target, res, path);
path.pop();
}