-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search_tree_iterator_ii.py
283 lines (221 loc) · 8.83 KB
/
binary_search_tree_iterator_ii.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
'''
Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):
BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false.
int next() Moves the pointer to the right, then returns the number at the pointer.
boolean hasPrev() Returns true if there exists a number in the traversal to the left of the pointer, otherwise returns false.
int prev() Moves the pointer to the left, then returns the number at the pointer.
Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST.
You may assume that next() and prev() calls will always be valid. That is, there will be at least a next/previous number in the in-order traversal when next()/prev() is called.
'''
Instead of precalcuating the whole inorder traversal, I have defined an iterative solution which would maintain the stack that would take an additional space of only O(h) where h is the height of the tree(h can be N in worst case).
Because we also need to track prev, we would need to store the list of nodes which are processed after inorder and idx to check where in the list we are.
So in all we need to initialize following instance variables:
stck - stores predecessors of a node
list - stores nodes which are processed after inorder. Remember that the cumulative space for list and stck would be O(N)
idx - stores the index of the iterator
class BSTIterator:
# T = O(h) where h is height of tree
def __init__(self, root: Optional[TreeNode]):
self.stck = [] # for tree traversal
self.list = [] # for storing the vals once it's processed
self.idx = -1 # ptr for self.list
# initialize stack with first left inorder
self.left_inorder(root)
# find predecessors for a node and update stck
def left_inorder(self, node):
while node:
self.stck.append(node)
node = node.left
# T = O(1)
def hasNext(self) -> bool:
return len(self.stck)>0 or self.idx < len(self.list)-1
# T = O(h)
def next(self) -> int:
if self.idx + 1 < len(self.list): # check if the idx is moved due to prev() calls
self.idx +=1
return self.list[self.idx]
else:
popped = self.stck.pop()
self.list.append(popped.val) # update list in inorder fashion
# perform left inorder if needed
if popped.right:
self.left_inorder(popped.right)
self.idx +=1
return popped.val
# T = O(1)
def hasPrev(self) -> bool:
return self.idx > 0
# T = O(1)
def prev(self) -> int:
self.idx -= 1
return self.list[self.idx]
Time complexity:
O(1) - for hasPrev(), prev() and hasNext() calls
O(h) - for constructor and next() where h is height of the tree (worst case h would be N)
Space complexity:
S = O(N) - for storing the list of all nodes in the tree
----------------------------------------------------------------------
Firstly, please refer to 173. BST iterator and try to solve it. Then, we only need to add a list that stores all the traversed values, and a pointer thats points to the current node, and then we can solve this question.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class BSTIterator(object):
def __init__(self, root):
"""
:type root: TreeNode
"""
self.stack = []
while root:
self.stack.append(root)
root = root.left
self.inorder = []
self.pointer = -1
def hasNext(self):
"""
:rtype: bool
"""
return self.pointer < len(self.inorder) - 1 or self.stack
def next(self):
"""
:rtype: int
"""
if 0 <= self.pointer < len(self.inorder) - 1:
self.pointer += 1
return self.inorder[self.pointer].val
#This part is simply BST iterator
node = self.stack[-1]
if node.right:
n = node.right
while n:
self.stack.append(n)
n = n.left
else:
n = self.stack.pop()
while self.stack and self.stack[-1].right == n:
n = self.stack.pop()
self.inorder.append(node)
self.pointer += 1
#print(node.val)
return node.val
def hasPrev(self):
"""
:rtype: bool
"""
return self.pointer >= 1
def prev(self):
"""
:rtype: int
"""
self.pointer -= 1
return self.inorder[self.pointer].val
----------------------------------------------------------
# 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 BSTIterator:
def __init__(self, root: TreeNode):
self.ans = []
stack = []
while True:
while root:
stack.append(root)
root = root.left
if not stack:
break
node = stack.pop()
self.ans.append(node.val)
root = node.right
self.i = -1
def hasNext(self) -> bool:
return self.i + 1 < len(self.ans)
def next(self) -> int:
self.i += 1
return self.ans[self.i]
def hasPrev(self) -> bool:
return self.i - 1 >= 0
def prev(self) -> int:
self.i-=1
return self.ans[self.i]
---------------------------------------------------------------------
class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.mem = []
self.ptr = 0
def rec(node):
if node:
if node.left:
rec(node.left)
self.mem.append(node.val)
if node.right:
rec(node.right)
rec(root)
def hasNext(self) -> bool:
return self.ptr < len(self.mem)
def next(self) -> int:
self.ptr += 1
return self.mem[self.ptr-1]
def hasPrev(self) -> bool:
#print(self.ptr)
return self.ptr > 1
def prev(self) -> int:
self.ptr -= 1
return self.mem[self.ptr-1]
----------------------------------------------------------
class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.stack = []
cur = root
while cur.left:
cur = cur.left
self._min = cur.val
cur.left = TreeNode(float('-inf'))
cur = root
while cur.right:
cur = cur.right
self._max = cur.val
cur.right = TreeNode(float('inf'))
cur = root
while cur:
self.stack.append(cur)
cur = cur.left
def hasNext(self) -> bool:
return self.stack[-1].val < self._max
def next(self) -> int:
if self.stack[-1].right:
# Has a right child, so we can find the next element in the right branch.
# Leave the last node in the stack as the smallest leaf in that branch.
cur = self.stack[-1].right
while cur:
self.stack.append(cur)
cur = cur.left
else:
# Go up to the first parent that is a left child of his parent,
# and get that higher parent as the last element in the stack.
last = self.stack.pop()
while self.stack[-1].right == last:
last = self.stack.pop()
return self.stack[-1].val
def hasPrev(self) -> bool:
return self.stack[-1].val > self._min
def prev(self) -> int:
if self.stack[-1].left:
# Has a left child, so we can find the prev element in the left branch.
# Leave the last node in the stack as the biggest leaf in that branch.
cur = self.stack[-1].left
while cur:
self.stack.append(cur)
cur = cur.right
else:
# Go up to the first parent that is a right child of his parent,
# and get that higher parent as the last element in the stack.
last = self.stack.pop()
while self.stack[-1].left == last:
last = self.stack.pop()
return self.stack[-1].val