Skip to content
AI360Xpert
Beta
Difficulty: MediumTrees

Validate Binary Search Tree

Problem in Plain English

Given the root of a binary tree, determine whether it is a valid binary search tree, where every node in the left subtree is smaller and every node in the right subtree is larger than the node. The rule must hold for every descendant, not just the immediate children.

Problem Statement

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST requires that the left subtree of a node contains only nodes with values less than the node's value, and the right subtree contains only nodes with values greater than the node's value. Both the left and right subtrees must also be valid binary search trees, and duplicate values are not allowed.

Constraints

  • The number of nodes in the tree is in the range [1, 10^4].
  • -2^31 <= Node.val <= 2^31 - 1
  • Duplicate values are invalid. Initial bounds must allow both signed 32-bit extremes.

Examples

Example 1

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

The left subtree contains only 1, which is below 2; the right contains only 3, which is above 2. Both children are leaves.

Example 2

Input
root = [-2147483648]
Output
true

A singleton has no ordering conflicts. The minimum signed 32-bit value is legal and must not collide with an artificial initial bound.

Example 3

Input
root = [5,1,7,null,null,4,8]
Output
false

Every immediate child comparison passes, including 4 being left of 7. But 4 is also in the right subtree of 5, where all values must exceed 5.

Intuition

A node must satisfy every ancestor, not only its parent. In Example 1, 1 belongs below 2 and 3 belongs above it. You can either inspect every descendant for each ancestor, or carry the intersection of ancestor requirements as an open interval. Strict boundaries reject duplicates.

Approaches

Naive Subtree Check

Solution Details

Reveal Naive Subtree Check: intuition, complexity, and code

Hints

Hint 1
A parent-child comparison cannot detect 4 below the right child 7 of root 5.
Hint 2
The left subtree must stay below its parent while keeping every older lower bound; the right subtree keeps older upper bounds.
Hint 3
Use strict open bounds wider than signed 32-bit values, and an explicit stack to handle a 10000-node chain.

Edge Cases

  • A singleton at either signed 32-bit extreme is valid.
  • A duplicate anywhere in an ancestor subtree fails the strict inequality.
  • A descendant can satisfy its parent yet violate the root, as Example 3 does.
  • A 10000-node skewed tree must not depend on a language recursion limit; explicit stacks handle it safely.

Common Mistakes and Interview Tips

  • Checking only immediate children loses ancestor restrictions.
  • Using inclusive comparisons accepts duplicates.
  • Using Integer.MIN_VALUE and Integer.MAX_VALUE as exclusive Java bounds rejects valid endpoints; use long bounds.
  • Subtracting one from a bound or comparing by subtraction risks integer overflow.
  • Replacing only the current parent bound rather than inheriting the opposite bound forgets older ancestors.

Key Takeaway

When a rule involves every ancestor rather than only the parent, carry the accumulated constraint through the traversal instead of repeatedly scanning subtrees.