We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 92bbc98 commit 20b9674Copy full SHA for 20b9674
Facebook/Problem#117.py
@@ -0,0 +1,29 @@
1
+"""
2
+Given a binary tree, return the level of the tree with minimum sum.
3
4
+def minLevelSum(root):
5
+ level = minLevel = 0
6
+ if not root:
7
+ return minLevel
8
+
9
+ queue = [[root]]
10
+ minSum = float('inf')
11
12
+ while queue:
13
+ level += 1
14
+ levelNodes = queue.pop(0)
15
+ nextLevel = []
16
+ levelSum = 0
17
+ while levelNodes:
18
+ node = levelNodes.pop()
19
+ levelSum +=node.val
20
+ if node.left:
21
+ nextLevel.append(node.left)
22
+ if node.right:
23
+ nextLevel.append(node.right)
24
+ if nextLevel:
25
+ queue.append(nextLevel)
26
+ if levelSum < minSum:
27
+ minSum = levelSum
28
+ minLevel = level
29
0 commit comments