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
# Created by Ketan Lalcheta
import heapq
from functools import total_ordering
@total_ordering # enables minimal comp operator overload definitions
class Node:
def __init__(self, d, n=None, b=None):
self.data = d
# multi purpose assignment that can handle Node(), int or None
self.next = Node(n) if type(n)==int else n if type(n)==Node else None
self.bottom = Node(b) if type(b)==int else b if type(b)==Node else None
# required by heapq for ordering
def __lt__(self, other):
return self.data < other.data
# suggested additional override definition if @total_ordering decorator is used.
def __eq__(self, other):
return self.data == other.data
class Solution:
def flatten(self, root):
if not root:
return None
pq = []
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run