/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
*
* 思路和比较常见的,就是js代码好像比别的语言简洁一些,尤其用了es6的扩展运算符
*/
function pathSum( root , sum ) {
// write code here
let arr = [];
function dfs(node, sum, path){
if(node==null){
return;
}
if(node.left == null && node.right == null && sum == node.val){
arr.push([...path, node.val]);
return;
}
if(node.left != null){
dfs(node.left, sum-node.val, [...path, node.val])
}
if(node.right != null){
dfs(node.right, sum-node.val, [...path, node.val])
}
}
dfs(root, sum, []);
return arr;
}
module.exports = {
pathSum : pathSum
};