-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathSum.py
More file actions
47 lines (39 loc) · 1.26 KB
/
PathSum.py
File metadata and controls
47 lines (39 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 112. Path Sum
# 113. Path Sum II
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False
# 当前节点为叶节点时,判断是否和剩余的sum相同
if not root.left and not root.right:
return root.val == sum
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
def pathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: List[List[int]]
"""
if not root:
return []
lists = []
# 对于叶节点,如果当前值等于剩余和,则将当前值加入列表并返回,否则返回空列表
if not root.left and not root.right:
if root.val == sum:
lists.append([root.val])
return lists
post = self.pathSum(root.left, sum - root.val) + self.pathSum(root.right, sum - root.val)
if post:
for list in post:
lists.append([root.val] + list)
return lists