''' 解题思路: 1、先dfs搜索,返回节点路径 2、当前为末端节点,且满足路径和条件,全局保存 ''' class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None # # # @param root TreeNode类 # @param sum int整型 # @return int整型二维数组 # class Solution: def pathSum(self , root , i ): # write code here if not root: return [] res = [root.val] out = [] def dfs(root,res): if not root.left and not root.right and sum(res)==i: out.append(res) if root.left: dfs(root.left,res+[root.left.val]) if root.right: dfs(root.right,res+[root.right.val]) dfs(root,res) #print('out=',out) return out ''' root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.right = TreeNode(5) root.right.left = TreeNode(6) root.right.right = TreeNode(7) print(Solution().pathSum(root,10)) '''