Introduction
LeetCode 98 – Validate Binary Search Tree is one of the most important Binary Search Tree interview problems.
This question is extremely popular because it tests:
- BST properties
- Recursive tree traversal
- DFS recursion
- Range validation
- Tree constraints handling
Many beginners make mistakes on this problem because checking only parent-child relationships is not enough.
Understanding the correct BST validation logic is very important for interviews.
Problem Link
🔗 https://leetcode.com/problems/validate-binary-search-tree/
Problem Statement
Given the root of a binary tree, determine whether it is a valid Binary Search Tree (BST).
A valid BST follows:
- Left subtree contains only smaller values
- Right subtree contains only greater values
- Both left and right subtrees must also be BSTs
Example 1
Input
Output
Explanation
All BST conditions are satisfied.
Example 2
Input
Output
Why False?
Although:
the node:
exists inside the right subtree of:
and should therefore be greater than 5.
This violates BST rules.
Key Insight
The most important understanding:
Every node must satisfy an entire valid range, not just parent comparison.
This is where many incorrect solutions fail.
Common Wrong Thinking
Many beginners try:
This is incorrect.
Because BST validation depends on:
not just immediate parent.
Correct Intuition
Each node has:
- Minimum allowed value
- Maximum allowed value
For example:
As recursion goes deeper:
Visual Understanding
Node:
is invalid because:
even though:
This proves:
Parent-only checking is insufficient.
Brute Force Approach
Idea
For every node:
- Find maximum in left subtree
- Find minimum in right subtree
- Validate BST conditions
Brute Force Complexity
Time Complexity
Because subtree traversal repeats for every node.
Space Complexity
Recursive stack space.
Optimized Recursive DFS Approach
The optimized idea:
Pass valid range during recursion.
Each node must satisfy:
Java Solution
Why Use Long Instead of Int?
Constraints allow:
Using:
can create edge-case failures.
So we safely use:
How This Works
For every node:
Left Subtree
Allowed range:
Right Subtree
Allowed range:
This guarantees global BST validity.
Dry Run
Input
Step 1
Current node:
Allowed range:
Valid.
Step 2
Move left:
Allowed range:
Valid.
Step 3
Move right:
Allowed range:
Valid.
Final Output
Another Dry Run
Input
Step 1
Node:
Range:
Valid.
Step 2
Move right to:
Range:
Now:
Invalid BST.
Final Output
Time Complexity Analysis
Time Complexity
Every node visited once.
Space Complexity
Where:
Worst case:
for skewed tree.
Alternative Approach Using Inorder Traversal
Key Property
BST inorder traversal produces:
Inorder Java Solution
Interview Explanation
In interviews, explain:
A valid BST requires every node to satisfy ancestor constraints, not just parent constraints. Therefore, we recursively maintain valid minimum and maximum bounds for each node.
This demonstrates:
- Deep BST understanding
- Recursive DFS mastery
- Constraint propagation
- Edge-case handling
Common Mistakes
1. Comparing Only Parent Nodes
Incorrect approach:
This misses ancestor violations.
2. Forgetting Strict Inequality
BST requires:
Duplicates are invalid.
3. Using int Instead of long
Can fail on edge values.
Always use:
4. Incorrect Range Passing
Correct recursion:
FAQs
Q1. Why does parent comparison fail?
Because BST validity depends on all ancestor constraints.
Q2. Why use min/max bounds?
Bounds propagate BST restrictions correctly.
Q3. Can inorder traversal solve this?
Yes.
BST inorder traversal must be strictly increasing.
Q4. Is this asked frequently?
Very frequently.
It is one of the most important BST interview questions.
Related Problems
Practice these next:
Conclusion
LeetCode 98 is an excellent problem for mastering:
- BST validation
- Recursive DFS
- Constraint propagation
- Tree traversal
- Interview problem-solving
The key insight is:
Every BST node must satisfy a valid global range, not just local parent conditions.
Once this concept becomes intuitive, many advanced BST problems become significantly easier.





