Is Binary Search Tree

Is Binary Search Tree

Return true if the tree is a valid binary search tree: for every node, all values in its left subtree are strictly smaller and all values in its right subtree are strictly larger.

isBinarySearchTree(root)

Input format: level-order with x for missing children. An empty tree is a valid BST.

Examples

5 3 8 2 4 7 9   // → true
5 3 8 2 6       // → false   (6 is in 5's left subtree)
(empty)         // → true

Walkthrough

Thinking it through

The tempting first attempt: check locally, each node's immediate left child is smaller, right child is larger. That's not enough. A node deep in a left subtree could be smaller than its immediate parent (passing the local check) but larger than some ancestor several levels up. The BST property is about a node versus every ancestor whose subtree it belongs to, not just its direct parent.

The fix: carry a valid range down the recursion

Each node needs to know the full range it's allowed to hold, based on every ancestor decision made to reach it — an open interval, tightened as you descend:

  • The root starts unbounded on both sides.
  • Descending into a left subtree tightens the upper bound to that node's value.
  • Descending into a right subtree tightens the lower bound to that node's value.

Building the approach

At each node, check its value strictly falls within the range handed down. If not, invalid — stop. If it does, recurse left with the upper bound tightened to this node's value, and right with the lower bound tightened. A missing node trivially satisfies any range. The tree is valid exactly when every node passes its check.

Why this works and the local check doesn't

Threading accumulated constraints from every ancestor means each node is checked against the full set of rules it must obey, not just its immediate parent's rule — catching violations against grandparents or higher ancestors. Each node does constant work, visited once — O(n) time, O(h) space for the recursion stack.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.