1. var hasPathSum = function(root, targetSum) {
    2. let res = false
    3. const backtrack = (node, count = 0) => {
    4. // root 为空或有找到结果则中断递归
    5. if(!node || res) return
    6. count += node.val
    7. // 判断如果是叶子节点,且等于目标值则直接改变状态,回退
    8. if(!node.right && !node.left) {
    9. if(count === targetSum) {
    10. res = true
    11. }
    12. return
    13. }
    14. if(node.left) {
    15. backtrack(node.left, count)
    16. }
    17. if(node.right) {
    18. backtrack(node.right, count)
    19. }
    20. }
    21. backtrack(root)
    22. return res
    23. };