Skip to content

Validate Binary Search Tree

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

A valid BST is defined as follows:

  • The left subtree of a node contains only nodes with keys strictly less than the node’s key.
  • The right subtree of a node contains only nodes with keys strictly greater than the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

Tree [2,1,3]

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

Example 2:

Tree [5,1,4,null,null,3,6]

Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4].
  • −231≤-2^{31} \le Node.val ≤231−1\le 2^{31} - 1

Comparing each node only with its own children is not enough. In the tree below every parent–child pair looks fine, yet 3 sits in the right subtree of 5 and is smaller than 5:

Tree 5, 4, 6, 3, 7 with each node's allowed range; 3 lies outside its range (5, 6)

The fix is to pass down the constraints of all ancestors. Each node gets an open interval (lo, hi) its value must lie in. The root may be anything. Going left from a node with value v keeps lo and sets hi = v (everything there must be smaller than v, and still satisfy the older bounds); going right sets lo = v. Each interval is the intersection of all the ancestor constraints on that path, so a tree is a BST exactly when every node lies inside its interval. In the picture, 3 must lie in (5, 6) and fails.

Node values span the full int range, so the “no bound yet” values must lie outside it; the bounds are long long starting at LLONG_MIN / LLONG_MAX.

  1. Call valid(root, LLONG_MIN, LLONG_MAX).
  2. In valid(node, lo, hi): a null node is valid, so return true.
  3. If node->val <= lo or node->val >= hi, return false (the bounds are strict, so duplicates are rejected).
  4. Return valid(node->left, lo, node->val) && valid(node->right, node->val, hi); && stops as soon as one side fails.
  • Time: O(n)O(n) — each node is checked once.
  • Space: O(h)O(h) — the recursion stack, where hh is the tree height (O(n)O(n) for a skewed tree, at most 10410^4 frames here).
  • Duplicate values ([2,2]): the child’s interval is (-∞, 2) and 2 <= 2 fails, as the strict definition requires.
  • Values at INT_MIN / INT_MAX ([2147483647]): they lie strictly inside (LLONG_MIN, LLONG_MAX), so a single extreme node is valid, and no val ± 1 arithmetic can overflow.

Think before you peek

Work out an approach on your own first — the explanation will still be here when you want it.

class Solution {
// Every value in this subtree must lie strictly between lo and hi.
bool valid(TreeNode* node, long long lo, long long hi) {
if (!node) return true;
if (node->val <= lo || node->val >= hi) return false;
return valid(node->left, lo, node->val) && valid(node->right, node->val, hi);
}
public:
bool isValidBST(TreeNode* root) { return valid(root, LLONG_MIN, LLONG_MAX); }
};
class Solution {
// Every value in this subtree must lie strictly between lo and hi.
private boolean valid(TreeNode node, long lo, long hi) {
if (node == null) return true;
if (node.val <= lo || node.val >= hi) return false;
return valid(node.left, lo, node.val) && valid(node.right, node.val, hi);
}
public boolean isValidBST(TreeNode root) {
return valid(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
}
class Solution:
def isValidBST(self, root: TreeNode | None) -> bool:
# Every value in a node's subtree must lie strictly between its lo and hi.
stack = [(root, float('-inf'), float('inf'))]
while stack:
node, lo, hi = stack.pop()
if not node:
continue
if node.val <= lo or node.val >= hi:
return False
stack.append((node.right, node.val, hi))
stack.append((node.left, lo, node.val))
return True

Give it a try first

Attempt the problem on LeetCode — the code will still be here.