Skip to main content

Find the Level of Tree with Minimum Sum - Solution & Explanation

MediumPremiumFree on FleetCodeTreeDepth-First SearchBreadth-First SearchBinary Tree8 min readAsked at: Microsoft
Practice this problem

Problem Statement

Given the root of a binary tree root where each node has a value, return the level of the tree that has the minimum sum of values among all the levels (in case of a tie, return the lowest level).

Note that the root of the tree is at level 1 and the level of any other node is its distance from the root + 1.

 

Example 1:

Input: root = [50,6,2,30,80,7]

Output: 2

Explanation:

Example 2:

Input: root = [36,17,10,null,null,24]

Output: 3

Explanation:

Example 3:

Input: root = [5,null,5,null,5]

Output: 1

Explanation:

 

Constraints:

  • The number of nodes in the tree is in the range [1, 105].
  • 1 <= Node.val <= 109

Approach Overview

Problem Overview: Given a binary tree, compute the sum of values at each level and return the level index that has the minimum total. Levels are processed from the root downward, so level 1 contains the root, level 2 contains its children, and so on.

Approach 1: Breadth-First Search / Level Order Traversal (O(n) time, O(w) space)

The most direct solution uses Breadth-First Search to process the tree level by level. Push the root into a queue, then repeatedly remove nodes from the queue while adding their children back. For each iteration, measure the current queue size to determine how many nodes belong to the current level, iterate through them, and compute the level sum.

Track two variables while traversing: the current level number and the smallest sum seen so far. After processing all nodes of a level, compare the computed sum with the minimum sum and update the answer if needed. Because every node is visited exactly once and each edge is processed once when adding children to the queue, the total runtime is O(n). The queue stores at most one level of nodes at a time, which requires O(w) space where w is the maximum tree width.

This approach fits naturally with problems involving level-based calculations in a binary tree. You avoid additional bookkeeping since the traversal order already groups nodes by level.

Approach 2: Depth-First Search with Level Tracking (O(n) time, O(h) space)

A second option uses Depth-First Search to accumulate sums per level. During recursion, pass the current depth and maintain an array or hash map where levelSum[depth] stores the total for that level. Each node contributes its value to the corresponding index before recursively exploring its left and right children.

After the traversal finishes, iterate through the stored level sums to find the smallest value and return its level index. The DFS visits each node once, giving O(n) time complexity. The recursion stack requires O(h) space where h is the tree height, plus the storage for level sums.

DFS is useful when you already perform recursive traversals or when the tree is narrow but deep. However, it requires an extra data structure to group nodes by level.

Recommended for interviews: The BFS level-order traversal is the expected solution. It directly mirrors the problem statement: process the tree one level at a time and compute a running sum. Showing the DFS alternative demonstrates deeper understanding, but the BFS implementation is simpler, avoids extra passes, and clearly communicates how level-based tree problems should be handled.

Solution

We can use Breadth-First Search (BFS) to traverse the binary tree level by level, record the sum of the node values at each level, and find the level with the smallest sum of node values, then return the level number.

The time complexity is O(n), and the space complexity is O(n). Where n is the number of nodes in the binary tree.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Breadth-First Search (Level Order)O(n)O(w)Best for level-based calculations and typical interview solutions
Depth-First Search with Level TrackingO(n)O(h)Useful when using recursive tree traversal or when storing level aggregates

Video Solution

3157. Find the Level of Tree with Minimum Sum (Leetcode Medium) • Programming Live with Larry • 196 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Find the Level of Tree with Minimum Sum easy or hard?
This problem is generally considered medium difficulty. The main challenge is recognizing that a level-order traversal cleanly separates nodes by depth, allowing straightforward computation of sums for each level.
Find the Level of Tree with Minimum Sum Python/Java solution
Most implementations use a queue-based BFS traversal. Python typically uses collections.deque, while Java uses a Queue or LinkedList to store nodes level by level. The algorithm calculates the sum for each level and keeps track of the level with the smallest sum.
How to solve Find the Level of Tree with Minimum Sum in O(n)?
Traverse the tree using a queue for level-order traversal. For each level, iterate through all nodes currently in the queue, compute their total sum, and push their children into the queue. Track the smallest level sum encountered and the corresponding level index. Since each node is processed once, the algorithm runs in O(n).
What is the best approach for Find the Level of Tree with Minimum Sum?
Breadth-First Search (level order traversal) is the best approach. It processes the binary tree one level at a time using a queue and computes the sum of node values for each level. Since every node is visited exactly once, the algorithm runs in O(n) time and naturally groups nodes by level.
Is Find the Level of Tree with Minimum Sum asked at Google/Amazon/Meta?
Tree traversal problems that involve computing values per level are common in interviews at companies like Google, Amazon, and Meta. Variants such as maximum level sum, average of levels, and level-based aggregations frequently appear in coding interviews to test BFS and binary tree understanding.
What data structure is used in Find the Level of Tree with Minimum Sum?
The primary data structure is a queue used for Breadth-First Search traversal of the binary tree. The queue allows nodes to be processed level by level. In DFS variants, an array or hash map is also used to store cumulative sums for each depth.
What is the time complexity of Find the Level of Tree with Minimum Sum?
The optimal solution runs in O(n) time where n is the number of nodes in the tree. Each node is visited once during traversal. The space complexity is O(w) for BFS where w is the maximum width of the tree, or O(h) for a DFS solution where h is the tree height.

Ready to solve this problem?

Practice Find the Level of Tree with Minimum Sum with our built-in code editor and test cases.

Practice on FleetCode