PY
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
# from code by Ketan Lalcheta
import heapq
class Node:
def __init__(self, d):
self.data = d
self.next = None
self.bottom = None
# if you create a class just to implement one method, just define a function instead.
def flatten(root):
if root:
pq = []
cur = root
while cur:
heapq.heappush(pq, (cur.data, cur))
cur = cur.next
last = Node(-1)
ans = last
while pq:
val, cur = heapq.heappop(pq)
last.bottom = cur
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run