题解 | #奶牛的喂养路径#

奶牛的喂养路径

https://www.nowcoder.com/practice/50ddeed545f3477e9864aaf0ff2f3007

function cowFeedingPath( root ,  targetSum ) {
    // write code here
    let ans = []
    if(!root) return []
    const traverse = (node,count,path) => {
        if(count == 0 && !node.left && !node.right) {
            ans.push([...path])
            return 
        }
        if(!node.left && !node.right)  {
            return 
        }
        if(node.left) {
            path.push(node.left.val)
            traverse(node.left,count-node.left.val,path)
            path.pop()
        }
        if(node.right) {
            path.push(node.right.val)
            traverse(node.right,count-node.right.val,path)
            path.pop()
        } 
    }
    traverse(root,targetSum-root.val,[root.val])
    return ans
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务