-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh272 O(nlogk).py
28 lines (22 loc) · 931 Bytes
/
h272 O(nlogk).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
# 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 closestKValues(self, root: Optional[TreeNode], target: float, k: int) -> List[int]:
hp = [] # schema: (-|difference|, value)
# maxheap so we pop
def dfs(curr: Optional[TreeNode], target: float, hp: list, k: int) -> None :
if not curr :
return
absDif = -abs(target - curr.val)
if len(hp) >= k :
heapq.heappushpop(hp, (absDif, curr.val))
else :
heapq.heappush(hp, (absDif, curr.val))
dfs(curr.left, target, hp, k)
dfs(curr.right, target, hp, k)
dfs(root, target, hp, k)
return [x[1] for x in hp]