Find the Maximum Root-to-Leaf Path Sum in a Binary Tree
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Overview: Find the maximum binary-tree root-to-leaf path sum with correct negative-value and single-child handling, a recurrence proof, and path reconstruction options.
Read the full Amazon Software Engineer interview experience this question came from
Constraints
- tree has 1 through 2^31 - 1 encoded slots, and its root exists.
- Node values and the exact maximum root-to-leaf sum are integers from -(2^53 - 1) to 2^53 - 1, inclusive.
- Other path sums and intermediate calculations may exceed the final result's range.
Examples
Input: ([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1],)
Expected Output: 27
Explanation: The path through values 5, 4, 11, and 7 has the greatest leaf sum.
Input: ([2, -5, 1, None, None, -9, -4],)
Expected Output: -1
Explanation: The best path is 2, 1, -4. Stopping at node 1 would fail to reach a leaf.
Hints
- When a node has exactly one child, identify which path sums can be compared at that node.