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 ce9b3b1

Browse files
Merge pull request #360 from MoigeMatino/add-tree-max-path-sum
feat: add tree max path sum
2 parents 52362c2 + e933b87 commit ce9b3b1

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ def max_path_sum(root):
4444
The time complexity is O(n), where n is the number of nodes in the binary tree, and the space complexity
4545
is O(h), where h is the height of the binary tree in the worst-case scenario.
4646
47-
TODO:
48-
Another iteration of this problem is the Binary Tree Maximum Path Sum
49-
This problem tries to find the max path sum that mmay or may not include
50-
the root. More on this...
47+
Another permutation of this problem is the Binary Tree Maximum Path Sum
48+
This problem tries to find the maximum sum of any non-empty path that may or may not include
49+
the root. Checkout the 'max_tree_path_sum' entry in this directory to find out how to solve this problem.
5150
"""
52-
"""
53-
54-
5551

‎binary_tree/max_tree_path_sum/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## **Problem Statement**
2+
3+
Given the root of a binary tree, return the maximum sum of any non-empty path.
4+
5+
A path in a binary tree is defined as follows:
6+
7+
A sequence of nodes in which each pair of adjacent nodes must have an edge connecting them.
8+
A node can only be included in a path once at most.
9+
Including the root in the path is not compulsory.
10+
11+
You can calculate the path sum by adding up all node values in the path. To solve this problem, calculate the maximum path sum given the root of a binary tree so that there won’t be any greater path than it in the tree.
12+
13+
14+
### Constraints
15+
16+
> 1 ≤ Number of nodes in the tree ≤ 500.
17+
18+
> −1000 ≤ Node.value ≤ 1000

‎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 によって変換されたページ (->オリジナル) /