leetcode113 -- Path Sum II

leetcode113 -- Path Sum II

题目

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
     / \
    4   8
   /   / \
  11  13  4
 /  \    / \
7    2  5   1
Return:

[
   [5,4,11,2],
   [5,8,4,5]
]


这个题目和之前的稍微有些不同,这个是不仅要判断是不是有这样的路径,还要把这样的路径给返回来。


# 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 = []
        self.helper(root, sum ,res, [])
        return res
        
    def helper(self, root, target, res, out):
        if not root:
            return 
        
        out.append(root.val)
        
        if not root.left and not root.right:
            if root.val == target:
                res.append(out[:])
                out.pop()
         
        elif not root.left and root.right:
            self.helper(root.right, target-root.val, res, out)       
        elif root.left and not root.right:
            self.helper(root.left, target-root.val, res, out)
        else:
            self.helper(root.left, target-root.val, res, out[:])
            self.helper(root.right, target-root.val, res, out[:])

我是这样想的,写的有些难看。 用一个res用来存放每组这样的路径,target子问题的和,out是一个缓存,只有这样的路径存在的时候才会被res给append起来,注意每次进来的时候只要root不为空都应该先把当前的值给append起来,如果是对的话,还需要把它再删掉,比如上面的例子中的,当走到11的时候,如果左边的是对的话,还需要判断右边的呢,所以要把7给删掉,让其他的遍历能够用前面的out,不然的话就会影响后面的使用,因为out一直在变化。

然后当有左右两支的时候不能传进去两个out, 而是要传递的是copy,不然右边地就不能够用了。

可以改成下面的


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

这个题当然也可以先把所有的从根到叶子的路径给找到,然后再判断。

# 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 = []
        self.helper(root, res ,[])
        ret = []
        for x in res:
            summ = 0
            for y in x:
                summ += y
                
            if summ==sum:
                ret.append(x)
                
        return ret
        
        
        
    def helper(self, root, res, out):
        if not root:
            return 
        
        out.append(root.val)
        if not root.left and not root.right:
            res.append(out[:])
            
        self.helper(root.left, res, out[:])
        self.helper(root.right, res,out[:])
        

打赏,谢谢~~

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,多谢支持~

打开微信扫一扫,即可进行扫码打赏哦