Level of the Minimum-Sum Subtree
Updated
Problem statement
For this exercise, assume a binary tree with signed integer values. The subtree rooted at a node contains that node and all its descendants. Find the nonempty subtree with the smallest sum of node values. Return the depth of that subtree’s root.
The whole tree's root has depth 0. If several subtrees have the same smallest sum, return the smallest root depth. Return -1 for an empty tree. The input is serialized in level order with null for missing children.
Function
minimumSubtreeLevel(root: TreeNode) → intExamples
Example 1
root = [5,-4,2,-3,1]return = 1The subtree rooted at -4 sums to -6, smaller than every other subtree sum. Its root is at depth 1.
Example 2
root = [0,0,0]return = 0Every subtree sum is zero. The shallowest minimum-sum root is the whole tree's root at depth 0.
Constraints
- For this exercise, assume the tree contains 0 to 1000 nodes.
- Node values are integers from
-10^9through10^9, and may repeat. - Use 64-bit subtree sums.
More PeopleStrong Phone Screen questions
- Reverse Linked-List Nodes in Groups of KOA · PHONE SCREEN · Seen Mar 2026
- Ceiling Value in a Sorted ArrayPHONE SCREEN · Seen Sep 2025
- Maximum Employee Salary for a CompanyPHONE SCREEN · Seen Sep 2025
- Valid ParenthesesPHONE SCREEN · Seen Sep 2025
- Number of ProvincesPHONE SCREEN · Seen May 2025
- Shortest Subarray with an Exact Target SumPHONE SCREEN · Seen May 2025
- Add Forward Linked-List Numbers Using RecursionPHONE SCREEN · Seen Oct 2021
- Running Median of a StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2021
- Build an Expression Tree from PostfixPHONE SCREEN · Seen Oct 2021
- Maximum Score from Deleting Neighboring ValuesPHONE SCREEN · Seen Oct 2021
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- House Robber IIIVisa · PHONE SCREENVisa interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions