Introduction
LeetCode 124 – Binary Tree Maximum Path Sum is one of the most important and frequently asked hard-level binary tree interview problems.
This problem teaches:
- Depth First Search (DFS)
- Bottom-up recursion
- Tree Dynamic Programming
- Recursive optimization
- Global answer tracking
It is considered a classic interview problem because it combines:
- Tree traversal
- Recursive decision making
- Path optimization
- Negative value handling
Mastering this problem helps in understanding advanced binary tree patterns used in:
- Diameter problems
- Tree DP
- Maximum path problems
- Graph recursion problems
Problem Link
🔗 https://leetcode.com/problems/binary-tree-maximum-path-sum/
Problem Statement
Given the root of a binary tree:
Return:
A path:
- Can start from any node
- Can end at any node
- Must follow parent-child connections
- Cannot visit a node more than once
Important:
Example 1
Input
Tree:
Output
Explanation:
Best path:
Path sum:
Example 2
Input
Tree:
Output
Explanation:
Best path:
Sum:
Key Observation
At every node:
We have two important possibilities.
Possibility 1
Path continues upward to parent.
In this case:
We can choose only:
because path cannot split upward.
Return value becomes:
Possibility 2
Current node becomes:
Then we can use:
This candidate updates global maximum answer.
Core Recursive Idea
At every node:
- Calculate left maximum contribution
- Calculate right maximum contribution
- Ignore negative paths
- Update global answer
- Return best single-side path upward
Why Ignore Negative Paths?
Negative paths reduce total sum.
So:
ensures:
- Negative contribution is discarded
- Only profitable paths are considered
This is the most important optimization.
Your Java Solution
Why This Works
For every node:
We calculate:
which is:
This path may become global maximum.
But while returning upward:
We can only choose:
because paths cannot split.
Dry Run
Input
Step 1
Leaf nodes:
Step 2
At node:
Left:
Right:
Current best path:
Update:
Return upward:
Step 3
At node:
Left:
Right:
Candidate:
Global maximum remains:
Final Answer
Recursive Visualization
Global maximum gets updated during return phase.
Brute Force Approach
A brute force approach would:
- Generate all possible paths
- Calculate sums
- Track maximum
But number of paths becomes extremely large.
This is inefficient.
Brute Force Complexity
Time Complexity
Can become:
or worse depending on implementation.
Optimized DFS Complexity
Time Complexity
because every node is visited once.
Space Complexity
where:
Worst case:
for skewed tree.
Important Insight
Two values exist at every node.
1. Value Returned Upward
Only one side allowed:
2. Value Used for Global Maximum
Both sides allowed:
This distinction is the heart of the problem.
Interview Explanation
In interviews, explain:
Every node acts as a potential highest point of a path. We compute the best path through that node while recursively returning the best single-side contribution upward.
This demonstrates:
- Advanced DFS understanding
- Tree DP concepts
- Recursive optimization
- Handling negative paths
Common Mistakes
1. Returning Both Sides Upward
Incorrect:
A path cannot branch upward.
2. Forgetting Negative Path Handling
Always use:
3. Assuming Path Must Pass Root
The path can exist entirely inside a subtree.
4. Not Using Global Variable
Maximum path may occur anywhere.
FAQs
Q1. Does path need to start from root?
No.
It can start and end anywhere.
Q2. Why ignore negative sums?
Negative paths reduce overall answer.
Q3. Why can we return only one side?
Because a path moving upward cannot split into two directions.
Q4. Is this problem important for interviews?
Extremely important.
It is one of the most famous hard-level tree problems.
Related Problems
After mastering this problem, practice:
Conclusion
LeetCode 124 is one of the best problems for learning advanced binary tree recursion.
It teaches:
- DFS optimization
- Tree dynamic programming
- Recursive decision making
- Negative path handling
- Global answer tracking
The key insight is:
Every node can become the highest point of a maximum path.
Once this recursive pattern becomes clear, many advanced tree and graph problems become much easier to solve.





