11. Maximum Path Sum
β GFG solution to the Maximum Path Sum problem: find maximum sum path between any two nodes in a binary tree using recursive DFS technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given the root
of a binary tree. Your task is to find the maximum path sum. The path may start and end at any node in the tree.
A path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
π Examples
Example 1
Input: root[] = [10, 2, 10, 20, 1, N, -25, N, N, N, N, 3, 4]
Output: 42
Explanation: The maximum path sum is 20 + 2 + 10 + 10 = 42
(represented by green nodes in the tree).
Example 2
Input: root[] = [-17, 11, 4, 20, -2, 10]
Output: 31
Explanation: The maximum path sum is 20 + 11 = 31
(represented by green nodes in the tree).
π Constraints
$1 \le \text{number of nodes} \le 10^3$
$-10^4 \le \text{node->data} \le 10^4$
β
My Approach
The optimal approach uses Recursive Depth-First Search (DFS) to efficiently calculate the maximum path sum:
Recursive DFS
Base Case:
If the current node is null, return 0 (no contribution to path sum).
Recursive Exploration:
Recursively calculate the maximum path sum from the left subtree.
Recursively calculate the maximum path sum from the right subtree.
Use
max(0, subtree_sum)
to ignore negative contributions (only include positive paths).
Update Global Maximum:
At each node, calculate the path sum that passes through the current node:
node->data + left_sum + right_sum
.Update the global maximum result with this value.
Return Value:
Return the maximum single path contribution going upward:
node->data + max(left_sum, right_sum)
.This allows the parent node to extend the path further.
Final Result:
After the complete traversal, return the global maximum path sum found.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of nodes in the binary tree. We visit each node exactly once during the DFS traversal.
Expected Auxiliary Space Complexity: O(h), where h is the height of the binary tree. This space is used by the recursion call stack. In the worst case (skewed tree), h can be O(n), and in the best case (balanced tree), h is O(log n).
π§βπ» Code (C++)
class Solution {
public:
int findMaxSum(Node *root) {
int res = INT_MIN;
dfs(root, res);
return res;
}
int dfs(Node *node, int &res) {
if (!node) return 0;
int l = max(0, dfs(node->left, res));
int r = max(0, dfs(node->right, res));
res = max(res, node->data + l + r);
return node->data + max(l, r);
}
};
β Code (Java)
class Solution {
int res;
int findMaxSum(Node root) {
res = Integer.MIN_VALUE;
dfs(root);
return res;
}
int dfs(Node node) {
if (node == null) return 0;
int l = Math.max(0, dfs(node.left));
int r = Math.max(0, dfs(node.right));
res = Math.max(res, node.data + l + r);
return node.data + Math.max(l, r);
}
}
π Code (Python)
class Solution:
def findMaxSum(self, root):
self.res = float('-inf')
def dfs(node):
if not node: return 0
l = max(0, dfs(node.left))
r = max(0, dfs(node.right))
self.res = max(self.res, node.data + l + r)
return node.data + max(l, r)
dfs(root)
return self.res
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated