FastPrepMaximum Root-to-Leaf Sum in a BST
Problems/Maximum Root-to-Leaf Sum in a BST

Maximum Root-to-Leaf Sum in a BST

Zillow logoZillow● EasyINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported at Zillow by a software engineering intern candidate in the United States who interviewed in October 2022 and did not get an offer. A first interview with a software engineer came roughly three weeks past the application, and a final round of two interviews in a row took place a bit more than two weeks after that; every interview opened with a few minutes of behavioral questions on Zillow's values, followed by a technical part of 35 to 40 minutes. The candidate listed five tasks without saying which interview had which; one was to find the largest sum along a path that begins at the root of a binary search tree.

The report does not say where the path ends, whether values can be negative, or how the tree reaches the function. These choices are ours: the path ends at a leaf (a node with no children), values can be negative, and the function maxRootToLeafSum gets the root of the tree, shown as a level-order list with null for a missing child, and returns the largest sum. The tree holds 1 to 10,000 nodes with distinct values from -10,000 to 10,000 and has at most 500 levels; these limits are ours as well.

Our version is about 85% the same as the reported question.

You are given the root of a binary search tree (BST): every value in a node's left subtree is smaller than the node's value, every value in its right subtree is larger, and no two nodes hold the same value. A node's subtree is the node together with everything below it.

A root-to-leaf path starts at the root and moves down one child at a time until it reaches a leaf, a node with no children. The sum of a path is the total of all values on it, the root and the leaf included.

Return the largest sum of any root-to-leaf path. Values can be negative, and a path must still end at a leaf, even when stopping higher up would give a larger total.

The examples write the tree as a level-order list: the root first, then each level from left to right, with null where a child is missing.

Function

maxRootToLeafSum(root: TreeNode) → int

Examples

Example 1

root = [10,5,20,2,8,null,null,null,null,null,9]return = 32
root
10528920

The tree has three root-to-leaf paths: 10 → 20 with sum 30, 10 → 5 → 2 with sum 17, and 10 → 5 → 8 → 9 with sum 32. The largest is 32. That path goes left at the root, although the largest value in the tree, 20, is on the right.

Example 2

root = [0,-3,null,-7,-1]return = -4
root
0-3-7-1

The root 0 has one child, -3, so the root is not a leaf. The two paths are 0 → -3 → -7 with sum -10 and 0 → -3 → -1 with sum -4, so the answer is -4. The root alone holds 0, which is larger, but a path has to end at a leaf.

Example 3

root = [-4]return = -4
root
-4

The root has no children, so it is a leaf. The only path is the root by itself, with sum -4.

Constraints

  • The number of nodes in the tree is between 1 and 104.
  • -104 <= Node.val <= 104
  • All values are distinct, and the tree is a valid binary search tree.
  • The tree has at most 500 levels.

More Zillow Phone Screen questions

  • Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
  • Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
  • Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
  • Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
  • Jump GamePHONE SCREEN · Seen Dec 2022
  • Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
  • Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
  • Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
  • Employee Free TimePHONE SCREEN · Seen Oct 2022
  • First Unique Character in a StringPHONE SCREEN · Seen Oct 2022

Similar questions at other companies

  • Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
  • Balanced Binary TreeGoogle · PHONE SCREENGoogle interview questions
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
  • Count Uni-Valued SubtreesAmazon · PHONE SCREEN
  • Validate Binary Search TreeLyft · PHONE SCREENLyft interview questions
  • Check Whether a Tree Is a Sum TreeAdobe · PHONE SCREENAdobe interview questions
  • Height-Balanced Binary TreeExpedia · ONSITE INTERVIEWExpedia interview questions
  • Sum Root-to-Leaf NumbersMeta · ONSITE INTERVIEWMeta interview questions
  • Binary Tree Maximum Path SumGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Balanced Tree Node ReportNetflix · ONSITE INTERVIEWNetflix interview questions
All Zillow interview questionsSee Zillow hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxRootToLeafSum(TreeNode root) {
        // Write your code here.
    }
}
Checking account…