题解 | #二叉树中和为某一值的路径(一)#

二叉树中和为某一值的路径(一)

https://www.nowcoder.com/practice/508378c0823c423baa723ce448cbfd0c

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

/**
  * 
  * @param root TreeNode类 
  * @param sum int整型 
  * @return bool布尔型
  */
function dfs(curNode ,sum){
    if(curNode == null){
        return false;
    }
    sum -= curNode.val;
    if(curNode.left == null && curNode.right == null && sum == 0){
        return true;
    }
    return dfs(curNode.left,sum) || dfs(curNode.right,sum);
 }
function hasPathSum( root ,  sum ) {
    // write code here
    if(root == null){
        return false;
    }
    return dfs(root,sum);

    //深度遍历二叉树 返回遍历数组
    // let stack = [];
    // let res = []
    // stack.push(root);
    // while(stack.length > 0){
    //     let node = stack.pop();
    //     if(node.right != null ){
    //         stack.push(node.right);
    //     }
    //     if(node.left != null){
    //         stack.push(node.left);
    //     }
    //     res.push(node.val);
    // }
    
}
module.exports = {
    hasPathSum : hasPathSum
};

#我的实习求职记录#
全部评论

相关推荐

Asp1rin:太有节目了哥
点赞 评论 收藏
分享
就是大飞舞:不能发,我暑假找实习的时候就被坑过,把你简历锁了,然后不给你推进度,你还投不了别的部门
点赞 评论 收藏
分享
笔试做完一个月了才约了明天的面试,kpi的几率大吗
要冲外企的祖国花朵很温柔:11点多才面完,感觉不像是kpi,问的也挺多的,面试官人也挺好的。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务