BM29 二叉树中和为某一值的路径(一)
/*
* 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
if(root === null){
return false
}
if(root.left === null && root.right === null){
if(sum - root.val === 0){
return true
}else{
return false
}
}
return hasPathSum(root.left,sum-root.val) || hasPathSum(root.right,sum-root.val)
}
module.exports = {
hasPathSum : hasPathSum
};

浙公网安备 33010602011771号