mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-06 08:45:11 -04:00
Rename is_valid_bst.py to bst_is_valid.py
This commit is contained in:
parent
7d6d6189e5
commit
ca5c15c9b6
1 changed files with 0 additions and 0 deletions
54
trees/bst_is_valid.py
Normal file
54
trees/bst_is_valid.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
def is_valid_bst_recursive(root):
|
||||
|
||||
def is_valid(root, min_val=float(-inf), max_val=float(inf)):
|
||||
if root is None:
|
||||
return True
|
||||
|
||||
return (min_val < root.val < max_val) and \
|
||||
is_valid(root.left, min_val, root.val) and \
|
||||
is_valid(root.right, root.val, max_val)
|
||||
|
||||
return is_valid(root)
|
||||
|
||||
|
||||
def is_valid_bst_iterative(root):
|
||||
|
||||
queue = deque()
|
||||
queue.append((root, float(-inf), float(inf)))
|
||||
|
||||
while queue:
|
||||
node, min_val, max_val = queue.popleft()
|
||||
if node:
|
||||
if min_val >= node.val or node.val >= max_val:
|
||||
return False
|
||||
if node.left:
|
||||
queue.append((node.left, min_val, node.val))
|
||||
if node.right:
|
||||
queue.append((node.right, node.val, max_val))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def is_valid_bst_inorder(root):
|
||||
|
||||
def inorder(node):
|
||||
if node is None:
|
||||
return True
|
||||
|
||||
inorder(node.left)
|
||||
queue.append(node.val)
|
||||
inorder(node.right)
|
||||
|
||||
queue = []
|
||||
inorder(root)
|
||||
for i in range(1, len(queue)):
|
||||
if queue[i] <= queue[i-1]:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue