/**
* 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 {boolean}
*/
var hasPathSum = function(root, targetSum) {
let result = false
let dfs = (data, presum, tar) => {
if (data) {
presum = data.val + presum
if (!data.left && !data.right) {
if (presum == tar) {
result = true
}
}
dfs(data.left, presum, tar)
dfs(data.right, presum, tar)
}
}
dfs(root, 0, targetSum)
return result
};