-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeRootToLeaf.py
More file actions
56 lines (44 loc) · 1.5 KB
/
Copy pathbinaryTreeRootToLeaf.py
File metadata and controls
56 lines (44 loc) · 1.5 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
48
49
50
51
52
53
54
55
56
# 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 TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def min_path_sum(root):
if not root:
return 0
def dfs(node, current_sum):
# Base case: if it's a leaf node, return the path sum
if not node.left and not node.right:
return current_sum + node.val
# Initialize left and right path sums with a large value
left_sum = float('inf')
right_sum = float('inf')
# Traverse left and right subtrees if they exist
if node.left:
left_sum = dfs(node.left, current_sum + node.val)
if node.right:
right_sum = dfs(node.right, current_sum + node.val)
# Return the minimum of the left and right path sums
return min(left_sum, right_sum)
# Start DFS traversal from the root with an initial sum of 0
return dfs(root, 0)
# Example usage:
# Construct the tree from the example
root = TreeNode(10)
root.left = TreeNode(5)
root.right = TreeNode(5)
root.left.right = TreeNode(2)
root.right.right = TreeNode(1)
root.right.right.left = TreeNode(-1)
# Call the function and print the minimum path sum
print(min_path_sum(root)) # Output: 15