98. Validate Binary Search Tree
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Trees
- Time
- O(N)
- Space
- O(H)
Standard Solution (C++)
SummaryGiven the root of a binary tree, determine whether it is a valid Binary Search Tree — meaning every node's value is strictly greater than all values in its left subtree and strictly less than all values in its right subtree, recursively throughout the entire tree.
Approach
- Propagate a valid [min, max) range down the tree during DFS: the root starts with an unbounded range, and each recursive call tightens the bounds.
- When descending left, the current node's value becomes the new upper bound; when descending right, it becomes the new lower bound.
- Using
long longsentinel values (LLONG_MIN/LLONG_MAX) instead of pointer comparisons cleanly handles integer-range edge cases without extra nullity checks. - Every node is visited exactly once, so the approach is linear in the number of nodes.
#include <climits>
using namespace std;
// TreeNode definition assumed provided by the judge.
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *l, TreeNode *r) : val(x), left(l), right(r) {}
};
class Solution {
public:
bool isValidBST(TreeNode* root) {
return validate(root, LLONG_MIN, LLONG_MAX);
}
private:
// Each node must satisfy: lo < node->val < hi
bool validate(TreeNode* node, long long lo, long long hi) {
if (!node) return true;
if (node->val <= lo || node->val >= hi) return false;
// Left subtree: values must be < node->val (tighten upper bound)
// Right subtree: values must be > node->val (tighten lower bound)
return validate(node->left, lo, node->val)
&& validate(node->right, node->val, hi);
}
}; Time O(n) Space O(h), where h is the tree height — O(log n) for a balanced tree, O(n) worst case for a skewed tree
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode* root, TreeNode* low, TreeNode* high){
if(!root){
return true;
}
if((high && root->val>=high->val) || (low && root->val<=low->val)){
return false;
}
return dfs(root->left, low, root) && dfs(root->right, root, high);
}
bool isValidBST(TreeNode* root) {
return dfs(root, nullptr, nullptr);
}
};
// Time Complexity: O(N)
// Space Complexity: O(H) where H is the height of the tree, since we are using recursion (the call stack will take up space).
// In the worst case (skewed tree), H = N. In the best case (balanced tree), H = log(N). Time O(N) Space O(H)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.