-
Notifications
You must be signed in to change notification settings - Fork 0
/
KthLargestSumInABinaryTree.py
50 lines (40 loc) · 1.73 KB
/
KthLargestSumInABinaryTree.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def kthLargestLevelSum(self, root: Optional[TreeNode], k: int) -> int:
bfs_order = deque([root])
min_heap = [] # maintain up to k largest values in a minimum heap
while bfs_order:
level_sum = 0
level_length = len(bfs_order)
for i in range(level_length):
cur_node = bfs_order.popleft()
level_sum += cur_node.val
if cur_node.left:
bfs_order.append(cur_node.left)
if cur_node.right:
bfs_order.append(cur_node.right)
heapq.heappush(min_heap, level_sum)
if len(min_heap) > k: # maintain up to size k in the minimum heap
heapq.heappop(min_heap)
return -1 if k > len(min_heap) else min_heap[0]
# solution with sorting the values instead of using a heap:
# class Solution:
# def kthLargestLevelSum(self, root: Optional[TreeNode], k: int) -> int:
# level_sum = defaultdict(int)
# bfs_order = deque([(root, 1)])
# while bfs_order:
# cur_node, cur_level = bfs_order.popleft()
# level_sum[cur_level] += cur_node.val
# if cur_node.left:
# bfs_order.append((cur_node.left, cur_level + 1))
# if cur_node.right:
# bfs_order.append((cur_node.right, cur_level + 1))
# if len(level_sum) < k:
# return -1
# sums_sorted = sorted(level_sum.values(), reverse=True)
# return sums_sorted[k - 1]