-
Notifications
You must be signed in to change notification settings - Fork 43
/
validate-binary-search-tree.py
430 lines (366 loc) · 12.9 KB
/
validate-binary-search-tree.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""
98. Validate Binary Search Tree
Medium
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
Example 1:
Input: root = [2,1,3]
Output: true
Example 2:
Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.
Constraints:
The number of nodes in the tree is in the range [1, 104].
-231 <= Node.val <= 231 - 1
"""
# V0
# IDEA : BFS
# -> trick : we make sure current tree and all of sub tree are valid BST
# -> not only compare tmp.val with tmp.left.val, tmp.right.val,
# -> but we need compare if tmp.left.val is SMALLER then `previous node val`
# -> but we need compare if tmp.right.val is BIGGER then `previous node val`
class Solution(object):
def isValidBST(self, root):
if not root:
return True
_min = -float('inf')
_max = float('inf')
### NOTE : we set q like below
q = [[root, _min, _max]]
while q:
for i in range(len(q)):
tmp, _min, _max = q.pop(0)
if tmp.left:
"""
### NOTE : below condition
### NOTE : we compare "tmp.left.val" with others (BEFORE we visit tmp.left)
"""
if tmp.left.val >= tmp.val or tmp.left.val <= _min:
return False
### NOTE : we append tmp.val as _max
q.append([tmp.left, _min, tmp.val])
if tmp.right:
"""
### NOTE : below condition
### NOTE : we compare "tmp.right.val" with others (BEFORE we visit tmp.right)
"""
if tmp.right.val <= tmp.val or tmp.right.val >= _max:
return False
### NOTE : we append tmp.val as _min
q.append([tmp.right, tmp.val, _max])
return True
# V0'
# IDEA : BFS
class Solution:
def isValidBST(self, root):
node_min = float('-inf')
node_max = float('inf')
bfs_queue = [(root, node_min, node_max)]
while bfs_queue:
node, node_min, node_max = bfs_queue.pop(0)
if node.left:
if node.left.val <= node_min or node.left.val >= node.val:
return False
bfs_queue.append((node.left, node_min, node.val))
if node.right:
if node.right.val <= node.val or node.right.val >= node_max:
return False
bfs_queue.append((node.right, node.val, node_max))
return True
# V0''
# IDEA: RECURSION
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return self.valid(root, float('-inf'), float('inf'))
def valid(self, root, min_, max_):
if not root: return True
if root.val >= max_ or root.val <= min_:
return False
return self.valid(root.left, min_, root.val) and self.valid(root.right, root.val, max_)
# V0'''
# IDEA: RECURSION
class Solution(object):
def isValidBST(self, root):
def valid(root, _max, _min):
if not root:
return True
if root.val >= _max or root.val <= _min:
return False
return valid(root.left, root.val, _min) and valid(root.right, _max, root.val)
return valid(root, float('inf'), -float('inf'))
# V0''''
# IDEA : BFS + Inorder traversal
class Solution:
def isValidBST(self, root):
pre, cur, stack = None, root, []
while stack or cur:
while cur:
stack.append(cur)
cur = cur.left
s = stack.pop()
if pre and s.val <= pre.val:
return False
pre, cur = s, s.right
return True
# V1
# IDEA : BFS
# https://leetcode.com/problems/validate-binary-search-tree/discuss/1532653/Python-BFS-solution-with-explanation-no-recursion
class Solution:
def isValidBST(self, root):
node_min = float('-inf')
node_max = float('inf')
bfs_queue = collections.deque([(root, node_min, node_max)])
while bfs_queue:
node, node_min, node_max = bfs_queue.popleft()
if node.left:
if node.left.val <= node_min or node.left.val >= node.val:
return False
bfs_queue.append((node.left, node_min, node.val))
if node.right:
if node.right.val <= node.val or node.right.val >= node_max:
return False
bfs_queue.append((node.right, node.val, node_max))
return True
# V1''
# IDEA : BFS
# https://leetcode.com/problems/validate-binary-search-tree/discuss/640837/Python-Good-use-case-for-BFS
class Solution:
def isValidBST(self, root):
if not root:
return True
QueueEntry = collections.namedtuple('QueueEntry', ['node', 'min', 'max'])
queue = deque([QueueEntry(root, float('-inf'), float('inf'))])
while queue:
node, min_bound, max_bound = queue.popleft()
if node.val <= min_bound or node.val >= max_bound:
return False
if node.left:
queue.append(QueueEntry(node.left, min_bound, node.val))
if node.right:
queue.append(QueueEntry(node.right, node.val, max_bound))
return True
# V1''' : TODO : figure out it
# IDEA : INORDER TRAVERSAL
# https://leetcode.com/problems/validate-binary-search-tree/discuss/166691/Python-solution
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
stack = []
trav = root
prev = -float('inf')
while trav or stack:
if trav:
stack.append(trav)
trav = trav.left
else:
u = stack.pop()
if u:
if u.val <= prev:
return False
prev = u.val
trav = u.right
return True
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/70209865
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return self.valid(root, float('-inf'), float('inf'))
def valid(self, root, min_, max_):
if not root: return True
if root.val >= max_ or root.val <= min_:
return False
return self.valid(root.left, min_, root.val) and self.valid(root.right, root.val, max_)
# V1'
# IDEA : BFS + Inorder traversal
# https://leetcode.com/problems/validate-binary-search-tree/discuss/32465/Python-Inorder-Traversal
class Solution:
def isValidBST(self, root):
pre, cur, stack = None, root, []
while stack or cur:
while cur:
stack.append(cur)
cur = cur.left
s = stack.pop()
if pre and s.val <= pre.val:
return False
pre, cur = s, s.right
return True
# V1'
# https://www.jiuzhang.com/solution/validate-binary-search-tree/#tag-highlight-lang-python
class Solution:
"""
@param root: The root of binary tree.
@return: True if the binary tree is BST, or false
"""
def isValidBST(self, root):
if root is None:
return True
stack = []
while root:
stack.append(root)
root = root.left
last_node = stack[-1]
while stack:
node = stack.pop()
if node.right:
node = node.right
while node:
stack.append(node)
node = node.left
# the only difference compare to an inorder traversal iteration
# this problem disallowed equal values so it's <= not <
if stack:
if stack[-1].val <= last_node.val:
return False
last_node = stack[-1]
return True
# V1''
# https://www.jiuzhang.com/solution/validate-binary-search-tree/#tag-highlight-lang-python
class Solution:
"""
@param root: The root of binary tree.
@return: True if the binary tree is BST, or false
"""
def isValidBST(self, root):
self.lastVal = None
self.isBST = True
self.validate(root)
return self.isBST
def validate(self, root):
if root is None:
return
self.validate(root.left)
if self.lastVal is not None and self.lastVal >= root.val:
self.isBST = False
return
self.lastVal = root.val
self.validate(root.right)
# V1'''
# IDEA : BFS + Inorder traversal
# https://leetcode.com/problems/validate-binary-search-tree/discuss/166691/Python-solution
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
stack = []
trav = root
prev = -float('inf')
while trav or stack:
if trav:
stack.append(trav)
trav = trav.left
else:
u = stack.pop()
if u:
if u.val <= prev:
return False
prev = u.val
trav = u.right
return True
# V1''''
# https://www.jiuzhang.com/solution/validate-binary-search-tree/#tag-highlight-lang-python
class Solution:
"""
@param root: The root of binary tree.
@return: True if the binary tree is BST, or false
"""
def isValidBST(self, root):
# write your code here
isBST, minNode, maxNode = self.divideConquer(root)
return isBST
def divideConquer(self, root):
if root is None:
return True, None, None
leftIsBST, leftMin, leftMax = self.divideConquer(root.left)
rightIsBST, rightMin, rightMax = self.divideConquer(root.right)
if not leftIsBST or not rightIsBST:
return False, None, None
if leftMax is not None and leftMax >= root.val:
return False, None, None
if rightMin is not None and rightMin <= root.val:
return False, None, None
# is BST
minNode = leftMin if leftMin is not None else root.val
maxNode = rightMax if rightMax is not None else root.val
return True, minNode, maxNode
# V1'''''
# IDEA : PY generators
# https://leetcode.com/problems/validate-binary-search-tree/discuss/715307/Python-generators-rule
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
def preorder(node):
if node is None:
return
yield from preorder(node.left)
yield node.val
yield from preorder(node.right)
val = float('-inf')
for nextval in preorder(root):
if nextval <= val:
return False
val = nextval
return True
# V2
# Time: O(n)
# Space: O(1)
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# Morris Traversal Solution
class Solution(object):
# @param root, a tree node
# @return a list of integers
def isValidBST(self, root):
prev, cur = None, root
while cur:
if cur.left is None:
if prev and prev.val >= cur.val:
return False
prev = cur
cur = cur.right
else:
node = cur.left
while node.right and node.right != cur:
node = node.right
if node.right is None:
node.right = cur
cur = cur.left
else:
if prev and prev.val >= cur.val:
return False
node.right = None
prev = cur
cur = cur.right
return True
# Time: O(n)
# Space: O(h)
class Solution2(object):
# @param root, a tree node
# @return a boolean
def isValidBST(self, root):
return self.isValidBSTRecu(root, float("-inf"), float("inf"))
def isValidBSTRecu(self, root, low, high):
if root is None:
return True
return low < root.val and root.val < high \
and self.isValidBSTRecu(root.left, low, root.val) \
and self.isValidBSTRecu(root.right, root.val, high)