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
};

  

posted @ 2022-03-28 09:11  方头小小狮  阅读(27)  评论(0)    收藏  举报