WorkspaceProblem lab
ALGORITHM VISUALIZER

Validate Binary Search Tree

Opening the interactive workspace…

Understanding Validate BST

Return whether a binary tree is a strict binary search tree: every left descendant is smaller and every right descendant is greater. Equal values are not permitted. Your function receives a TreeNode or null.

How it works

Every ancestor contributes a bound; comparing only immediate children is insufficient.

Pass an exclusive lower and upper bound into each recursive call and tighten one bound for each child.

Try an example

Input: values = [5, 1, 4, null, null, 3, 6]

Output: false

Node 4 lies in the right subtree of 5 but is smaller than 5.

Constraints

  • At most 31 level-order entries.
  • Integer node values are between −1,000,000 and 1,000,000.
  • An empty tree is valid.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity