Check if a Binary Tree is a Binary Search Tree

Problem Statement

Problem Statement

Given the root node of a binary tree, determine whether it satisfies the properties of a Binary Search Tree (BST).

  • In a BST, for every node:
    • All nodes in its left subtree have values less than the node's value.
    • All nodes in its right subtree have values greater than the node's value.
  • Additionally, both the left and right subtrees must also be binary search trees.

Examples

Input Tree Is BST? Description
[10, 5, 15, 2, 7, 12, 20]
true All nodes satisfy BST properties: left subtree < root < right subtree at every node.
[10, 5, 8]
false Right child 8 of root 10 violates BST rule since 8 < 10.
[5]
true Single node is a valid BST by definition.
[] true An empty tree is considered a valid BST.
[10, 5, 15, 1, 12, 6, 20]
false Node 12 in left subtree of 15 violates BST rule since it's less than 15 but appears in right subtree.
[8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13]
true All left & right subtrees maintain valid ranges. This is a classic valid BST example.

Visualization Player

Solution

Solution Explanation

Case 1: The Tree is Empty

An empty tree (i.e., the root node is null) is considered a valid Binary Search Tree by definition. Since there are no nodes, the BST property is trivially satisfied.

Answer: true

Case 2: The Tree is a Valid BST

If the tree follows all the rules of a BST—where for each node, all values in the left subtree are strictly less than the node, and all values in the right subtree are strictly greater—it qualifies as a valid BST.

This condition must hold not just at the root, but throughout the tree. For example, in the tree:

        2
       / \
      1   3
    

Both children follow the rules with respect to the root 2, and since they don’t have further children, the validation ends successfully.

Answer: true

Case 3: The Tree Violates BST Rules

A tree may appear locally valid but fail to follow BST rules deeper down the subtree. For instance:

        5
       / \
      1   4
         / \
        3   6
    

Here, the node 3 is in the right subtree of 5 but is less than 5. This violates the BST property that all right children (and their descendants) must be greater than the root.

Answer: false

Case 4: Subtle Violations from Deep Nodes

Sometimes violations are not obvious at the first level. Consider this tree:

        10
       / \
      5  15
         / \
        6  20
    

At first glance, all immediate children seem valid. However, node 6 is in the right subtree of 10 but is less than 10, which breaks the BST rule.

Such errors can only be detected by checking the value of each node against a range defined by its ancestors—not just its immediate parent.

Answer: false

Case 5: Perfectly Valid and Balanced BST

In a complete BST like this:

        8
       / \
      4   12
     / \  / \
    2  6 10 14
    

Each node correctly places its children to the left (smaller) and right (greater), and this is true recursively throughout the tree. It passes all BST checks.

Answer: true

Algorithm Steps

  1. If the tree is empty, return true.
  2. For each node, check if its value is within the valid range (min and max).
  3. If the node's value does not satisfy min < node.val < max, return false.
  4. Recursively verify the left subtree with an updated upper bound (max = node.val) and the right subtree with an updated lower bound (min = node.val).
  5. If all nodes satisfy the BST property, return true.

Code

Python
Java
JavaScript
C
C++
C#
Kotlin
Swift
Go
Php
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def isBST(root, min_val=float('-inf'), max_val=float('inf')):
    if not root:
        return True
    if not (min_val < root.val < max_val):
        return False
    return isBST(root.left, min_val, root.val) and isBST(root.right, root.val, max_val)

# Example usage:
if __name__ == '__main__':
    # Construct BST:      2
    #                   /   \
    #                  1     3
    root = TreeNode(2, TreeNode(1), TreeNode(3))
    print(isBST(root))