Created
April 28, 2018 19:51
-
-
Save amulyakashyap09/84491e9a874a8ea75e45c4a562d10136 to your computer and use it in GitHub Desktop.
validates whether tree is binary search tree or not
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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