/*
 * function TreeNode(x) {
 *   this.val = x;
 *   this.left = null;
 *   this.right = null;
 * }
 */

/**
  * 
  * @param root TreeNode类 
  * @param sum int整型 
  * @return bool布尔型
  */
function hasPathSum(root, sum) {
    // write code here
    function hasPathSum(node, cnt) {
        if (node == null) {
            return false;
        }
        cnt += node.val;
        // 当前节点为叶子节点并且目标路径存在,返回 true
        if (node.left == null && node.right == null && cnt == sum) {
            return true;
        }
        return hasPathSum(node.left, cnt) || hasPathSum(node.right, cnt);
    }
    if (root == null) {
        return false;
    }
    let ans = hasPathSum(root, 0);
    return ans;
}
module.exports = {
    hasPathSum: hasPathSum
};