一、题目地址
二、具体代码
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
function hasPathSum( root , sum ) {
//1、如果root是空树,直接返回
if(root === null) {
return false;
}
if((root.val === sum) && (root.left === null) && (root.right === null)) {
return true;
}
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
module.exports = {
hasPathSum : hasPathSum
};