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

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

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

class Solution:
    def FindPath(self, root: TreeNode, target: int) -> List[List[int]]:
        def dfs(root, target,tmp):
            if not root:
                return []
            if root.left:
                dfs(root.left, target-root.val, tmp+[root.val])
            if root.right:
                dfs(root.right, target-root.val, tmp+[root.val])
            if target == root.val and not root.left and not root.right:
                tmp += [root.val]
                res.append(tmp)
        res = []
        tmp = []
        dfs(root,target,[])
        return res

全部评论

相关推荐

犹豫的小狐狸刷了100道题:你是我在牛课上见到的最漂亮的女孩了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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