首页 > 其他 > 详细

二叉树中和为某一值的所有结果

时间:2021-03-08 14:13:31      阅读:22      评论:0      收藏:0      [点我收藏+]
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
        res = []
        path = []
        def find_path(root, target):
            if not root: return []
            path.append(root.val)
            target -= root.val
            if target == 0 and root.left is None and root.right is None:
                res.append(path[::])
            
            find_path(root.left, target)
            find_path(root.right, target)
            path.pop()
        find_path(root, sum)
        return res

二叉树中和为某一值的所有结果

原文:https://www.cnblogs.com/KbMan/p/14498134.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!