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
import heapq
class Node:
def __init__(self, d):
self.data = d
self.next = None
self.bottom = None
#this is required as
#priority queue is having two memebers
#int and class
#if int is same, it compares other member that is class and
#this method is needed for class comparision
def __lt__(self,other):
return False
class Solution:
def flatten(self, root):
if not root:
return None
pq = []
cur = root
while cur:
heapq.heappush(pq, (cur.data, cur))
cur = cur.next
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run