Quick 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.

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

Given a nonempty binary tree with numeric node values, return the greatest sum along a path that starts at the root and ends at a leaf. A leaf has no children. Negative values are allowed. ### Constraints & Assumptions The source says maximum root-to-leaf path without specifying whether maximum means node-value sum or path length. This practice exercise explicitly chooses numeric sum; confirm that interpretation. A longest-path-by-length task would use a different objective. ### Clarifying Questions to Ask - Does maximum mean the sum of node values or the number of nodes/edges? - Must the path end at a leaf, even if every continuation reduces the sum? - Should an empty tree return no result or raise an error? ```hint Missing children are not zero-cost exits A root-to-leaf path cannot stop early at an internal node merely to avoid negative descendants. ``` ### What a Strong Answer Covers - A recursive best suffix from each node to a leaf. - Distinguishing a missing child from a valid zero-sum path. - Single-child nodes, all-negative trees, and stack depth. ### Follow-up Questions - How would you return the actual path as well as its sum? - Why would a leaf-to-leaf maximum require different state?

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

Given a nonempty binary tree with integer node values, return the maximum sum of node values along a path from the root to a leaf. A leaf has no children. The sum includes both the root and the leaf. A path must end at a leaf even if continuing lowers its sum. The input `tree` encodes the tree in level order. The first entry is the root. For each existing node in level order, the next two slots represent its left and right children. A null slot represents a missing child and consumes no child slots of its own. Trailing null slots can be omitted.

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

  1. When a node has exactly one child, identify which path sums can be compared at that node.

Find the Maximum Root-to-Leaf Path Sum in a Binary Tree

hardAmazonSoftware Engineer

Given a nonempty binary tree with integer node values, return the maximum sum of node values along a path from the root to a leaf. A leaf has no children. The sum includes both the root and the leaf. A path must end at a leaf even if continuing lowers its sum.

The input tree encodes the tree in level order. The first entry is the root. For each existing node in level order, the next two slots represent its left and right children. A null slot represents a missing child and consumes no child slots of its own. Trailing null slots can be omitted.

Examples

Example 1
Input
([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1],)
Output
27
Notes
The path through values 5, 4, 11, and 7 has the greatest leaf sum.
Example 2
Input
([2, -5, 1, None, None, -9, -4],)
Output
-1
Notes
The best path is 2, 1, -4. Stopping at node 1 would fail to reach a leaf.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def max_root_to_leaf_sum(tree: list[int | None]) -> int:
Sample call
max_root_to_leaf_sum([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1])
Language
Case 1
Input
([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1],)
Expected
27
Case 2
Input
([2, -5, 1, None, None, -9, -4],)
Expected
-1

Browse More Questions

Show the approach

Approach

Traverse the encoding in level order. Keep a FIFO queue of path sums, one for each discovered existing node whose children have yet to be read. Initially the queue contains the root value. Dequeue the next parent sum and consume up to two child slots. For each existing child, enqueue the parent sum plus the child's value. If neither child exists, the parent is a leaf, so compare its path sum with the best leaf sum seen so far.

The order of queued sums matches the order in which existing parents consume slots in the encoding. Each enqueued sum includes precisely the values along that child's root path. Only leaves are candidates, so a missing child or a negative continuation cannot end a path early.

Path sums can exceed the final result's transport range. Python and JavaScript use arbitrary-precision integers, Java uses BigInteger, and C++ uses a 128-bit integer. A tree of fewer than 2^31 nodes with values of magnitude below 2^53 has path sums of magnitude below 2^84.

Time complexity:
O(n) for n encoded slots.
Space complexity:
O(n) auxiliary space for the traversal.