Maximum Root-to-Leaf Sum in a BST
Updated
Problem statement
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) → intExamples
Example 1
root = [10,5,20,2,8,null,null,null,null,null,9]return = 32The 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 = -4The 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 = -4The 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
1and104. -104 <= Node.val <= 104- All values are distinct, and the tree is a valid binary search tree.
- The tree has at most
500levels.
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