-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_580.py
44 lines (36 loc) · 1018 Bytes
/
problem_580.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
"""
This question was asked by Apple.
Given a binary tree, find a minimum path sum from root to a leaf.
For example, the minimum path in this tree is [10, 5, 1, -1], which has sum 15.
10
/ \
5 5
\ \
2 1
/
-1
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def PathSum(root: TreeNode):
if not root:
return False
if not root.left and not root.right:
return root.val
res = root.val
res += min(PathSum(root.left), PathSum(root.right))
return res
if __name__ == '__main__':
root = TreeNode(10)
root.left = TreeNode(5)
root.left.right = TreeNode(2)
root.right = TreeNode(5)
root.right.right = TreeNode(1)
root.right.right.left = TreeNode(-1)
print(PathSum(root))
# Time Complexity = O(n) where n is the number of nodes in the tree
# Space Complexity = O(n)