-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath-sum-iii.js
More file actions
31 lines (25 loc) · 847 Bytes
/
path-sum-iii.js
File metadata and controls
31 lines (25 loc) · 847 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
/**
* 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 === null ) return 0;
return dfs(root, targetSum) + pathSum(root.left, targetSum) + pathSum(root.right, targetSum);;
};
// dfs() is just the count starting from the specified node,
// and need to add counts starting from it's children.
function dfs( node, targetSum ){
let res = 0;
if( node === null ) return 0;
if( node.val === targetSum ) res++;
return res + dfs(node.left, targetSum-node.val) + dfs(node.right, targetSum-node.val);
};