Maximum Sum Path Between Two Leaf Nodes
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt at this Google question, reported in July 2026, is copying the classic max path sum solution and letting the path end at any node. Here both endpoints must be leaves, and that changes everything. It's a tree DFS problem with a post-order return value, and the negative-value example is there to punish the shortcut. If you're taking this OA soon, learn the one-line difference between the two versions. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the trick is small enough to carry in your head.
The problem
Given the root of a binary tree whose nodes contain integer values, return the maximum sum of the node values along a simple path whose two endpoints are distinct leaves. A leaf is a node with no children. The input is guaranteed to contain at least two leaves, so a valid leaf-to-leaf path always exists. The path may pass through any common ancestor, does not need to pass through the root, and may contain negative values. Its sum includes both leaf endpoints and every intermediate node. Function maxLeafToLeafPathSum(root: TreeNode) → long Examples Example 1 root = [1,2,3] return = 6 The only leaf-to-leaf path is 2 - 1 - 3, whose node values sum to 6. Example 2 root = [-10,9,20,null,null,15,7] return = 42 The maximum path is 15 - 20 - 7, whose node values sum to 42. It does not pass through the root. Example 3 root = [-1,-2,-3] return = -6 All values are negative, but the path must still connect the two leaves. The path -2 - -1 - -3 sums to -6. Constraints The tree contains between 3 and 100000 nodes, inclusive. -10^9 <= node.val <= 10^9 The tree contains at least two distinct leaves. Every leaf-to-leaf path sum fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Run a post-order DFS. For each node, return the best sum of a downward path from that node to some leaf in its subtree. At a leaf, that's just its value. At a node with two children, compute left + node.val + right as a candidate answer, then return node.val + max(left, right). The pitfall: a node with only one child can't close a leaf-to-leaf path, so don't update the answer there. Just return node.val plus the single child's value. Also don't clamp negatives to zero like the classic problem does, because the path must reach a leaf no matter what. Initialize the answer to negative infinity, not 0, since Example 3 returns -6. Use 64-bit math. With up to 100000 nodes, a skewed tree can blow a recursive stack in some languages, so consider an iterative post-order. StealthCoder is the hedge if the recursion edge cases slip under time pressure in the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Maximum Sum Path Between Two Leaf Nodes cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Sum Path Between Two Leaf Nodes FAQ
What's the trick in the Google maximum leaf-to-leaf path sum question?+
Return the best downward path to a leaf from each node, and only update the global answer when a node has two children. Combine left + node + right as the candidate. Don't clamp negative child values to zero, because leaves are mandatory endpoints.
How is this different from LeetCode's Binary Tree Maximum Path Sum?+
The classic version lets the path start and end at any node and ignores negative branches. This one forces both endpoints to be leaves, so you can't drop a negative subtree. Single-child nodes also can't finish a path. Same DFS shape, different rules.
Why does a node with one child need special handling?+
A path through a single-child node can't have two leaf endpoints within that node's subtree, so it can't be a valid candidate there. Just pass node.val plus the child's best downward sum up to the parent, and skip updating the answer.
What should I initialize the answer to?+
Negative infinity, or the smallest 64-bit value. Example 3 returns -6, so starting at 0 gives a wrong result on all-negative trees. Sums can exceed 32-bit range with values up to 10^9 and 100000 nodes, so use long.
How do I prepare for this in 48 hours?+
Write the post-order DFS from scratch twice and test it on the three examples, especially the all-negative one. Then try a skewed chain and a tree where one node has a single child. Know the iterative post-order fallback in case deep recursion is an issue.