Validate Binary Search Tree

DFS passes a (min, max) bound interval — each node's value must lie strictly inside.

Medium
Time O(n) Space O(h)
LeetCode
3 min read
tree dfs bst

Problem#

Given the root of a binary tree, return whether it’s a valid BST: left subtree’s values strictly less than node, right subtree strictly greater, both subtrees themselves valid.

Examples#

  • [2,1,3]true
  • [5,1,4,null,null,3,6]false

Constraints#

  • The number of nodes is in [1, 10^4]. -2^31 <= Node.val <= 2^31 - 1.

Approach#

DFS carrying a (lo, hi) exclusive range. Each node must satisfy lo < val < hi. Recurse into left with hi = val, into right with lo = val. Use long long bounds (or pointers) to handle int extremes.

Solution#

Validate Binary Search Tree
class Solution {
public:
bool isValidBST(TreeNode* root) {
function<bool(TreeNode*, long, long)> dfs = [&](TreeNode* u, long lo, long hi) {
if (!u) return true;
if (u->val <= lo || u->val >= hi) return false;
return dfs(u->left, lo, u->val) && dfs(u->right, u->val, hi);
};
return dfs(root, LONG_MIN, LONG_MAX);
}
};

Editorial#

A common bug is only checking direct children — but BST validity is global: a node’s right subtree must all stay below its parent’s bound. Threading the interval through DFS captures this perfectly.

Complexity#

  • Time: O(n).
  • Space: O(h).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.