Home
DSA Patterns
Depth-First Search
Validate Binary Search Tree
Validate Binary Search Tree (LeetCode 98)
Validate Binary Search Tree (LeetCode 98): determine if a binary tree is a valid Binary Search Tree (BST).
Examples
Left (1) < 2 < Right (3).
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:
- Every key anywhere in its left subtree is less than its own key.
- Every key anywhere in its right subtree is greater than its own key.
- 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)
- Pass a valid range
(min, max)down to each node. - Root range:
(-Infinity, +Infinity). - When going Left, update max:
(min, node.val). - When going Right, update min:
(node.val, max). - If a node's value is not within
(min, max), returnfalse.
Interactive Visualization
EACH NODE MUST FIT (LOW, HIGH)
CALL valid(root)
Range
:
Valid BST
:
call stack
No calls in progress
Call valid(root) with no lower and no upper bound
Next
valid(5): it must lie strictly inside (−∞, +∞)
Tree
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
Longornullfor infinity bounds if node values can beINT_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 }910 return valid(root, null, null);11};
Validate Binary Search Tree in Python, Java, C++, TypeScript and C
Validate Binary Search Tree in Python
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def valid(node, low, high):
if not node:
return True
if (low is not None and node.val <= low) or (high is not None and node.val >= high):
return False
return valid(node.left, low, node.val) and valid(node.right, node.val, high)
return valid(root, None, None)Validate Binary Search Tree in Java
class Solution {
public boolean isValidBST(TreeNode root) {
return valid(root, null, null);
}
private boolean valid(TreeNode node, Integer low, Integer high) {
if (node == null) return true;
if ((low != null && node.val <= low) || (high != null && node.val >= high)) {
return false;
}
return valid(node.left, low, node.val) && valid(node.right, node.val, high);
}
}Validate Binary Search Tree in C++
class Solution {
public:
bool isValidBST(TreeNode* root) {
return valid(root, nullptr, nullptr);
}
bool valid(TreeNode* node, TreeNode* low, TreeNode* high) {
if (!node) return true;
if ((low && node->val <= low->val) || (high && node->val >= high->val)) {
return false;
}
return valid(node->left, low, node) && valid(node->right, node, high);
}
};Validate Binary Search Tree in TypeScript
function isValidBST(root: TreeNode | null): boolean {
function valid(node: TreeNode | null, low: number | null, high: number | null): boolean {
if (!node) return true;
if ((low !== null && node.val <= low) || (high !== null && node.val >= high)) {
return false;
}
return valid(node.left, low, node.val) && valid(node.right, node.val, high);
}
return valid(root, null, null);
}Validate Binary Search Tree in C
static bool valid(struct TreeNode* node, struct TreeNode* low, struct TreeNode* high) {
if (node == NULL) return true;
if ((low != NULL && node->val <= low->val) || (high != NULL && node->val >= high->val)) {
return false;
}
return valid(node->left, low, node) && valid(node->right, node, high);
}
bool isValidBST(struct TreeNode* root) {
return valid(root, NULL, NULL);
}More practice: DSA interview questions with answers · patterns cheat sheet
