var hasPathSum = function(root, targetSum) {
let res = false
const backtrack = (node, count = 0) => {
// root 为空或有找到结果则中断递归
if(!node || res) return
count += node.val
// 判断如果是叶子节点,且等于目标值则直接改变状态,回退
if(!node.right && !node.left) {
if(count === targetSum) {
res = true
}
return
}
if(node.left) {
backtrack(node.left, count)
}
if(node.right) {
backtrack(node.right, count)
}
}
backtrack(root)
return res
};