Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 16acdd2

Browse files
feat: add max tree path sum
1 parent 52362c2 commit 16acdd2

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed
File renamed without changes.

‎binary_tree/max_path_root_sum/max_path_root_sum.py renamed to ‎binary_tree/max_root_leaf_path_sum/max_root_leaf_path_sum.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,4 @@ def max_path_sum(root):
4949
This problem tries to find the max path sum that mmay or may not include
5050
the root. More on this...
5151
"""
52-
"""
53-
54-
5552

‎binary_tree/max_tree_path_sum/__init__.py

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
def max_tree_path_sum(root):
2+
"""
3+
Calculate the maximum path sum in a binary tree. A path is defined as any sequence of nodes
4+
from some starting node to any node in the tree along the parent-child connections. The path
5+
does not need to go through the root, and it can start and end at any node.
6+
7+
Parameters:
8+
root (Node): The root node of the binary tree.
9+
10+
Returns:
11+
int: The maximum path sum found in the binary tree.
12+
"""
13+
def max_contrib(node):
14+
if not node:
15+
return 0, float("-inf")
16+
17+
# Recursively calculate the maximum contributions and path sums for left and right subtrees
18+
left_contrib, left_max = max_contrib(node.left)
19+
right_contrib, right_max = max_contrib(node.right)
20+
21+
# Ensure only non-negative contributions are considered
22+
left_contrib = max(left_contrib, 0)
23+
right_contrib = max(right_contrib, 0)
24+
25+
# Calculate the maximum path sum with the current node as the highest node
26+
current_max_sum = node.val + left_contrib + right_contrib
27+
28+
# Determine the maximum path sum found so far
29+
max_sum = max(current_max_sum, left_max, right_max)
30+
31+
# Calculate the maximum contribution to the parent node
32+
node_contrib = node.val + max(right_contrib, left_contrib)
33+
return node_contrib, max_sum
34+
35+
# Start the recursion and return the maximum path sum found
36+
_, max_sum = max_contrib(root)
37+
38+
return max_sum
39+
40+
# Approach and Reasoning:
41+
# -----------------------
42+
# The function uses a helper function `max_contrib` to recursively calculate two values for each node:
43+
# 1. `node_contrib`: The maximum path sum that can be extended to the parent node. This is the node's
44+
# value plus the maximum contribution from either its left or right subtree, ensuring that only
45+
# non-negative contributions are considered.
46+
# 2. `max_sum`: The maximum path sum found so far, which could be the maximum path sum through the
47+
# current node (including both left and right children) or the best path sum found in the left or
48+
# right subtrees.
49+
50+
# The algorithm uses postorder traversal to ensure that each node's subtrees are fully processed before
51+
# calculating the path sum that includes the node itself; allows us to gather information from the bottom up.
52+
# This is crucial for correctly determining the
53+
# maximum path sum that might pass through any node in the tree.
54+
55+
# Time Complexity:
56+
# ----------------
57+
# The time complexity of this algorithm is O(n), where n is the number of nodes in the binary tree.
58+
# This is because each node is visited exactly once during the traversal.
59+
60+
# Space Complexity:
61+
# -----------------
62+
# The space complexity is O(h), where h is the height of the tree. This is due to the recursive call
63+
# stack. In the worst case (a completely unbalanced tree), the height h can be n, leading to a space
64+
# complexity of O(n). In a balanced tree, the height is log(n), leading to a space complexity of O(log n).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /