-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path135_Apple_Find_MinPath_Sum_From_Root_To_Leaf.py
executable file
·75 lines (58 loc) · 1.69 KB
/
135_Apple_Find_MinPath_Sum_From_Root_To_Leaf.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
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
"""
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def get_min_path(root):
"""
Finds the min path from root to leaf node in a binary tree
Args:
root: Root of the tree
Returns: "min_path" -> the path with minimum sum from root to leaf
>>> tree = Node(10,\
left=Node(5,\
right=Node(2)),\
right=Node(5,\
right=Node(1,\
left=Node(-1))))
>>> print(get_min_path(tree))
[10, 5, 1, -1]
>>> tree = Node(-1)
>>> print(get_min_path(tree))
[-1]
"""
min_sum = float('inf')
min_path = []
def helper(root, curr_path=[]):
nonlocal min_path
if root.left:
helper(root.left, curr_path + [root.data])
if root.right:
helper(root.right, curr_path + [root.data])
if root.left is None and root.right is None:
if sum(curr_path) < min_sum:
min_path = curr_path + [root.data]
# print(min_path)
helper(root)
return min_path
if __name__ == '__main__':
import doctest
doctest.testmod()
tree = Node(10,
left=Node(5,
right=Node(2)),
right=Node(5,
right=Node(1,
left=Node(-1))))