您的位置:首页 > 其它

[LeetCode]Path Sum II

2015-07-28 20:31 393 查看
太简单,不解释
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
# @param {TreeNode} root
# @param {integer} sum
# @return {integer[][]}
def pathSum(self, root, sum):
if not root:
return []

res = []
self.dfs(root, sum, [], res)
return res

def dfs(self, root, sum, ls, res):
if not root.left and not root.right:
if root.val == sum:
ls.append(root.val)
res.append(ls)
if root.left:
self.dfs(root.left, sum-root.val, ls+[root.val], res)
if root.right:
self.dfs(root.right, sum-root.val, ls+[root.val], res)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: