FDEInterviews logoFDE/Interviews
Coding & DSA / 83
hardMetaAmazonGoogle

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.

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.

Unlock the other 466 answers · ₹2,000 / $25Your progress and mastery stay saved · 6 months · one payment · no auto-renew
UP NEXT ON YOUR JOURNEY
FEDITOR'S NOTE

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.

DISCUSSION · 0

No comments yet — be the first to share your approach.