Check if a Binary Tree is a BST

Given the root of a binary tree, determine whether it is a binary search tree by checking all structural and value constraints. Pattern focus: BST Validation with Bounds. A correct solution must validate every subtree with inherited bounds rather than checking only parent-child pairs.

Input Format

root = binary tree root

Output Format

true if the tree is a BST

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^9 <= node values <= 10^9
  • root must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [10,5,15,null,null,6,20]

Output:

false

Explanation:

Node 6 is in the right subtree of 10 but is smaller than 10.

Example 2:

Input:

root = [8,4,10,2,6,9,12]

Output:

true

Explanation:

All nodes satisfy the BST constraints.

Loading...
Check if a Binary Tree is a BST