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

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

https://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
var path = [];
var res = [] //存放最终结果
function dfs(root,expectNumber){
    if(root == null){
        return;
    }
    expectNumber -= root.val;
    path.push(root.val); //存放每条路径
    if(expectNumber == 0 && root.left == null && root.right == null){
        res.push(path);
        //下面三句防止push()深拷贝 改为浅拷贝
        temp = path;
        path = [];
        for(let i = 0;i < temp.length;i++){
            path.push(temp[i]);
        }
    }
    dfs(root.left,expectNumber);
    dfs(root.right,expectNumber);
    path.pop(); //回溯 先记住 dfs最后一步都有
}
function FindPath(root, expectNumber)
{
    // write code here
    if(root == null){
        return [];
    }
    dfs(root,expectNumber);
    return res;
}
module.exports = {
    FindPath : FindPath
};

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

相关推荐

一表renzha:你点进去没打招呼他也会有提示的,之前我点进美的,还没打招呼,他马上给我发了不太合适哦
点赞 评论 收藏
分享
04-29 18:07
常州大学 Java
寂静羽翼:兄弟我已经亲身经历了,双非没实习很多大厂还是会给笔试的,可是有的公司笔试做的好也不给面一直卡着,ssob基本看我没实习都拒绝我了,但是每天投满偶尔也能有一两场初创公司的面试,但是薪资基本在五六千
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务