A path can start and end anywhere and bends through at most one node, values can be negative, and you want the maximum sum. The move is a DFS that returns the best downward gain (clamped at zero) while a global max tracks the best path that bridges through each node.
Maximum path sum in a binary tree
A path can start and end anywhere and bends through at most one node, values can be negative, and you want the maximum sum. The move is a DFS that returns the best downward gain (clamped at zero) while a global max tracks the best path that bridges through each node.
Updated Aug 2026 · Grounded in real Forward Deployed Engineer interview loops and written to a senior-engineer editorial bar.
This is the hard cousin of tree diameter and the pattern is identical: return one value up the recursion, update a global as you pass through each node. The two things that separate a pass from a stall are clamping negative subtree contributions to zero, and keeping the return value (a single downward arm) distinct from the global update (the full bend through the node). Negative-only trees are the input that breaks naive 'sum everything' attempts.
No comments yet — be the first to share your approach.
