Skip to content

Instantly share code, notes, and snippets.

@amulyakashyap09
Created April 28, 2018 19:51
Show Gist options
  • Save amulyakashyap09/84491e9a874a8ea75e45c4a562d10136 to your computer and use it in GitHub Desktop.
Save amulyakashyap09/84491e9a874a8ea75e45c4a562d10136 to your computer and use it in GitHub Desktop.
validates whether tree is binary search tree or not
""" Node is defined as
class node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
"""
def isBSTUtil(node, min, max):
if node.data <= min:
return False
if node.data >= max:
return False
left_ok = True
right_ok = True
if node.left is not None:
left_ok = isBSTUtil(node.left, min, node.data)
if node.right is not None:
right_ok = isBSTUtil(node.right, node.data, max)
return left_ok and right_ok
def checkBST(root):
if root is None:
return True
return (isBSTUtil(root, float('-inf'), float('inf')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment