Talentd
FleetCode

3157. Find the Level of Tree with Minimum Sum

Medium69.6% acceptance1 companyPremium elsewhere, free here

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

Asked at 1 company

Hints open one at a time

Hint 1
Run a DFS on the tree and update an array sum where sum[i] is the sum for level i.
Hint 2
The answer is the first minimum element of sum.
5:13
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[50,6,2,30,80,7]
Input
[36,17,10,null,null,24]
Input
[5,null,5,null,5]
Your code is saved on this device.Solution