Watch 10 video solutions for Binary Tree Maximum Path Sum, a hard level problem involving Dynamic Programming, Tree, Depth-First Search. This walkthrough by take U forward has 420,687 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
The path sum of a path is the sum of the node's values in the path.
Given the root of a binary tree, return the maximum path sum of any non-empty path.
Example 1:
Input: root = [1,2,3] Output: 6 Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
Example 2:
Input: root = [-10,9,20,null,null,15,7] Output: 42 Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
Constraints:
[1, 3 * 104].-1000 <= Node.val <= 1000Problem Overview: Given a binary tree, compute the maximum sum of any path. A path can start and end at any node, but it must follow parent-child connections. The path does not need to pass through the root.
Approach 1: Recursive Depth-First Search (O(n) time, O(h) space)
This approach performs a post-order traversal using depth-first search. For every node, compute the maximum contribution it can send to its parent. That contribution is node.val + max(leftGain, rightGain), where negative gains are treated as zero. At each node, also evaluate a candidate path that passes through the node using leftGain + node.val + rightGain and update a global maximum. Each node is processed once, giving O(n) time complexity and O(h) recursion stack space where h is tree height. This approach works well because it separates two ideas: the best downward path contribution and the best full path passing through a node.
Approach 2: Dynamic Programming with Tree Traversal (O(n) time, O(h) space)
This method frames the same traversal as a dynamic programming problem on a binary tree. Each node stores the best downward path sum that can extend to its parent. During traversal, compute two values: the best downward extension and the best path passing through the current node using both children. The DP transition is straightforward: ignore negative child paths, update the global maximum with the combined path, and return the best single-branch extension upward. The algorithm still visits each node exactly once, so time complexity remains O(n), with recursion stack space O(h).
Recommended for interviews: The recursive DFS solution is what most interviewers expect. It clearly demonstrates understanding of tree traversal and how to maintain global state during recursion. Explaining why negative child sums are ignored and how the "split path" is evaluated at each node shows strong problem-solving depth.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Depth-First Search | O(n) | O(h) | Standard solution for interview settings; simple recursion with global max tracking |
| Dynamic Programming with Tree Traversal | O(n) | O(h) | When framing the problem as tree DP and explaining state transitions |