Skip to content

Commit 5893eaf

Browse files
committed
路径之和
1 parent 9b02d1b commit 5893eaf

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tree/113_pathSum.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ def helper(root, total):
4646
helper(root, total)
4747
return ans
4848

49+
def pathSum_dfs_2(self, root: TreeNode, total: int) -> List[List[int]]:
50+
def dfs(root, tmp, total):
51+
if not root:
52+
return
53+
if not root.left and not root.right and root.val == total:
54+
tmp += [root.val]
55+
ans.append(tmp)
56+
dfs(root.left, tmp + [root.val], total - root.val)
57+
dfs(root.right, tmp + [root.val], total - root.val)
58+
59+
ans = []
60+
dfs(root, [], total)
61+
return ans
62+
4963

5064
if __name__ == '__main__':
5165
lst = [1, 2, 3]

0 commit comments

Comments
 (0)