leetcode -- Path Sum II --- 重点,未理解

https://leetcode.com/problems/path-sum-ii/

没有太理解。要再看看

参考:
http://www.cnblogs.com/zuoyuan/p/3752503.html

自己重写code

class Solution(object):

    def dfs(self, root, target, valuelist, res):
        if root.left == None and root.right == None:
            if root.val == target:
                res.append(valuelist + [root.val])
        else:
            if root.left:
                self.dfs(root.left, target - root.val, valuelist + [root.val], res)
            if root.right:
                self.dfs(root.right, target - root.val, valuelist + [root.val], res)

    def pathSum(self, root, sum):
        """ :type root: TreeNode :type sum: int :rtype: List[List[int]] """
        if not root: return []
        res = []
        self.dfs(root, sum, [], res)
        return res

你可能感兴趣的:(LeetCode)