Pattern this teaches:Tree DFS
Binary Tree Maximum Path Sum
A hard Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 2
- Core for
- 4 roles
The problem
Given the root of a binary tree, find the maximum path sum. A path is defined as any sequence of nodes connected by parent-child edges, where no node appears more than once. The path must contain at least one node and does not need to go through the root.
Example 1
- Input
- [3,2,4,1,null,null,5]
- Output
- 15
- Why
- Best path: 1-2-3-4-5 = 15. Left branch 1-2-3, right branch 3-4-5.
Example 2
- Input
- [-8,12,6,3,9,null,14]
- Output
- 33
- Why
- The best path 9-12-(-8)-6-14 sums to 33.
Example 3
- Input
- [-3]
- Output
- -3
- Why
- A single node tree has a path sum equal to that node's value.
Constraints
- The number of nodes is in the range [1, 3 * 104].
- -1000 <= Node.val <= 1000
How to think about it
Updated 2026-09-29A path can fork through a node to link its left and right descendants into an arch, but that arch cannot be extended upwards to an ancestor without splitting the path. Therefore, each node computes the best un-split branch sum to offer its parent, while simultaneously evaluating the full arch sum against the global record.
Approaches, worst first
Recursive path exploration
time O(n^2) · space O(h)
Compute all possible downward paths from every node, testing all pairs of descendants. Explores combinatorial path combinations repeatedly with severe runtime penalties.
Bottom-up branch contributionWrite this one
time O(n) · space O(h)
At each node, compute left and right subtree contributions clamped at 0 to drop negative paths. Update global answer with `node.val + left + right`. Return `node.val + max(left, right)` to parent. Solves all candidates in a single pass.
Where people lose marks · 3
- Initializing global max sum to 0 breaks on trees containing only negative numbers; it must start at negative infinity or `root.val`.
- Not clamping negative child branch sums with `Math.max(0, branch)` forces negative subtrees to drag down the total path sum.
- Returning `node.val + left + right` to the caller instead of extending only a single branch creates an illegal branching path.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .