-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0112_path-sum.py
55 lines (46 loc) · 1.44 KB
/
0112_path-sum.py
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
48
49
50
51
52
53
54
55
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def hasPathSum(self, root, targetSum):
"""
:type root: TreeNode
:type targetSum: int
:rtype: bool
"""
if root is None:
return False
targetSum = targetSum - root.val
if targetSum == 0 and root.left is None and root.right is None:
# targetSum equal Zero and No child nodes
return True
return self.hasPathSum(root.left, targetSum) or self.hasPathSum(root.right, targetSum)
if __name__ == '__main__':
# example 1
# False
# root = TreeNode(1, TreeNode(2), TreeNode(3))
# targetSum = 5
# example 2
# True
root = TreeNode(5,
TreeNode(4,
TreeNode(11,
TreeNode(7),
TreeNode(2))),
TreeNode(8,
TreeNode(13),
TreeNode(4,
left=None,
right=TreeNode(1)))
)
targetSum = 22
# example 3
# False
# root = TreeNode(1, TreeNode(2))
# targetSum = 1
s = Solution()
b = s.hasPathSum(root, targetSum)
print(b)