<jupyter_start><jupyter_text>Using min and max in another wayJust passing minrange and maxrange                 r                / \               /   \        min, r-1    r, max<jupyter_code>import queue
class BinaryTreeNode:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None

def minTree(root):
    if root == None:
        return 1000000
    leftMin = minTree(root.left)
    rightMin = minTree(root.right)
    return min(leftMin, rightMin, root.data)

def maxTree(root):
    if root == None: 
        return -10000000
    leftMax = maxTree(root.left)
    rightMax = maxTree(root.right)
    return max(leftMax, rightMax, root.data)

def isBST(root):
    if root == None:
        return True
    leftMax = maxTree(root.left)
    rightMin = minTree(root.right)
    if root.data > rightMin or root.data <= leftMax:
        return False

    isLeftBST = isBST(root.left)
    isRightBST = isBST(root.right)

    return isLeftBST and isRightBST

def isBST2(root):
    if root == None:
        return 1000000, -1000000, True
    leftMin, leftMax, isLeftBST = isBST2(root.left)
    rightMin, rightMax, isRightBST = isBST2(root.right)
    
    minimum = min(leftMin, rightMin, root.data)
    maximum = max(leftMax, rightMax, root.data)
    
    isTreeBST = True
    
    if root.data <= leftMax or root.data > rightMin:
        isTreeBST = False
    if not(isLeftBST) or not(isRightBST):
        isTreeBST = False
        
    return minimum, maximum, isTreeBST
    
def isBST3(root, min_range, max_range):
    if root == None:
        return True
    if root.data < min_range or root.data > max_range:
        return False
    
    isLeftWithinConstraints = isBST3(root.left, min_range, root.data -1)
    isRightWithinConstraints = isBST3(root.right, root.data, max_range)
    
    return isLeftWithinConstraints and isRightWithinConstraints
    
    

def printTreeDetailed(root):
    if root == None:
        return
    print(root.data, end = ":")

    if root.left is not None:
        print(root.left.data, end = ",")

    if root.right is not None:
        print(root.right.data, end = " ")

    print()
    printTreeDetailed(root.left)
    printTreeDetailed(root.right)
    
def takeLevelWiseTreeInput():
    q = queue.Queue()
    print("Enter root")
    rootData = int(input())
    if rootData == -1:
        return None
    root = BinaryTreeNode(rootData)
    q.put(root)
    
    while (not(q.empty())):
        current_node = q.get()
        
        print("Enter left child of ", current_node.data)
        leftChildData = int(input())
        if leftChildData != -1:
            leftChild = BinaryTreeNode(leftChildData)
            current_node.left = leftChild
            q.put(leftChild)
            
        print("Enter right child of ", current_node.data)
        rightChildData = int(input())
        if rightChildData != -1:
            rightChild = BinaryTreeNode(rightChildData)
            current_node.right = rightChild
            q.put(rightChild)
            
    return root
root = takeLevelWiseTreeInput()
printTreeDetailed(root)
isBST3(root, -10000, 10000)<jupyter_output>Enter root
4
Enter left child of  4
2
Enter right child of  4
6
Enter left child of  2
1
Enter right child of  2
3
Enter left child of  6
5
Enter right child of  6
7
Enter left child of  1
-1
Enter right child of  1
-1
Enter left child of  3
-1
Enter right child of  3
-1
Enter left child of  5
-1
Enter right child of  5
-1
Enter left child of  7
-1
Enter right child of  7
-1
4:2,6 
2:1,3 
1:
3:
6:5,7 
5:
7:
{'fraction_non_alphanumeric': 0.09118967452300786, 'fraction_numerical': 0.026094276094276093, 'mean_word_length': 2.313197026022305, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '3587', 'license': 'Unlicense', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1173, 'n_words': 336, 'path': '15 BST-1/15.7 isBST (Another solution).ipynb', 'repo_name': 'suhassuhas/Coding-Ninjas---Data-Structures-and-Algorithms-in-Python', 'size': '3564'}