Browse curriculum

Validate Binary Search Tree

Determine if a binary tree is a valid Binary Search Tree (BST).

Examples

Input:root = [2,1,3]
Output:true
Explain:

Left (1) < 2 < Right (3).

Input:root = [5,1,4,null,null,3,6]
Output:false
Explain:

5 is the root, yet its right child holds 4, a smaller key on the right side, so this is not a BST.

Problem Understanding

Check whether the binary tree under root is a valid Binary Search Tree (BST).

That holds when, at every node:

  1. Every key anywhere in its left subtree is less than its own key.
  2. Every key anywhere in its right subtree is greater than its own key.
  3. Both rules hold again inside each subtree.

Algorithm Strategy

Common Mistake: Just checking left.val < node.val and right.val > node.val is wrong. It fails for deeper levels (e.g., a node in the right subtree can be smaller than the root even though every parent-child pair looks correct).

Correct Approach: Range Validation (Top-Down DFS)

  1. Pass a valid range (min, max) down to each node.
  2. Root range: (-Infinity, +Infinity).
  3. When going Left, update max: (min, node.val).
  4. When going Right, update min: (node.val, max).
  5. If a node's value is not within (min, max), return false.

Interactive Visualization

Each node is checked against the open range it was handed, shown under it. Going left caps the range at the parent’s value; going right raises its floor. The first node outside its range turns red.

Dry Run: [5, 1, 4, null, null, 3, 6]

Root: 5

Node Current Range Check Action
5 (-Inf, +Inf) Valid Check Left, Check Right
1 (Left) (-Inf, 5) 1 < 5? Yes Valid.
4 (Right) (5, +Inf) 4 > 5? NO Invalid! Return False

Edge Cases & Common Mistakes

  • Equal Values: BST definition usually implies strict inequality. Check problem constraints. Usually left < node < right.
  • Integer Overflow: Use Long or null for infinity bounds if node values can be INT_MAX. The code below uses no bound at all (None / null) instead of an infinity, so it works for every integer value.

Solution

Range Validation approach.

Validate Binary Search Tree Code
1var isValidBST = function(root) {
2 function valid(node, low, high) {
3 if (!node) return true;
4 if ((low !== null && node.val <= low) || (high !== null && node.val >= high)) {
5 return false;
6 }
7 return valid(node.left, low, node.val) && valid(node.right, node.val, high);
8 }
9
10 return valid(root, null, null);
11};

The approach, step by step

  1. Start with no bounds

    Call valid(root) with no lower and no upper bound.

  2. Check the range

    Return false if the node is not strictly between low and high.

  3. Narrow the range for the children

    Check the left child against (low, node.val) and the right child against (node.val, high).

Frequently asked questions

Why is comparing each node with its children not enough?

A node can be correct relative to its parent but wrong relative to an ancestor. In [5, 4, 6, null, null, 3, 7], 3 is a fine left child of 6 but sits in 5’s right subtree, where every value must be greater than 5.


How does the range check work?

Every node must lie strictly inside (low, high). Going left, the node’s value becomes the new upper bound; going right, it becomes the new lower bound.


Are duplicate values allowed?

Not in this problem. The ranges are open, so a value equal to a bound fails, and [2, 2, 2] is not a valid BST.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description