-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProblem#117.py
More file actions
29 lines (27 loc) · 739 Bytes
/
Problem#117.py
File metadata and controls
29 lines (27 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
Given a binary tree, return the level of the tree with minimum sum.
"""
def minLevelSum(root):
level = minLevel = 0
if not root:
return minLevel
queue = [[root]]
minSum = float('inf')
while queue:
level += 1
levelNodes = queue.pop(0)
nextLevel = []
levelSum = 0
while levelNodes:
node = levelNodes.pop()
levelSum +=node.val
if node.left:
nextLevel.append(node.left)
if node.right:
nextLevel.append(node.right)
if nextLevel:
queue.append(nextLevel)
if levelSum < minSum:
minSum = levelSum
minLevel = level
return minLevel