98. Validate Binary Search Tree - Detailed Explanation
Problem Statement
Given the root of a binary tree, determine if it is a valid Binary Search Tree (BST). A valid BST satisfies:
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- Both left and right subtrees must also be BSTs.
Examples
2
/ \
1 3
- Input: [2,1,3]
- Output: true
5
/ \
1 4
/ \
3 6
- Input: [5,1,4,null,null,3,6]
- Output: false
- Explanation: 3 is in the right subtree of 5 but less than 5
Constraints
- The number of nodes in the tree is in the range [1, 10⁴].
- –2³¹ ≤ Node.val ≤ 2³¹−1
Hints
- An in‑order traversal of a BST visits nodes in strictly increasing order.
- You can recursively verify each node’s value against allowed
(min, max)
bounds passed down from its ancestors. - Be careful with integer limits when using bounds.
Approaches
In‑Order Traversal (Recursive)
Perform a DFS in‑order (left → node → right), keeping track of the previously visited value. If at any point the current node’s value is ≤ the previous value, it violates the BST property.
Steps
- Initialize
prev = None
(or-∞
). - Traverse left subtree.
- When visiting a node, compare
node.val
toprev
. Ifprev
is notNone
andnode.val ≤ prev
, return false. - Update
prev = node.val
. - Traverse right subtree.
- If no violations, return true.
Step‑by‑Step
For tree [5,1,4,null,null,3,6]:
- In‑order visits 1 → 5 → 3 → …
- At 3, compare to
prev=5
: 3 ≤ 5 → invalid.
Range‑Check Recursion
At each node, carry down a valid open interval (low, high)
that the node’s value must lie within.
- Start with
(low, high) = (−∞, +∞)
. - For node with value
v
, checklow < v < high
. - Recurse left with
(low, v)
. - Recurse right with
(v, high)
. - If any check fails, return false.
Step‑by‑Step
For the invalid tree:
- Root=5 in (−∞,∞) → OK
- Right child=4 must be in (5,∞). 4 ≤ 5 → invalid.
Iterative In‑Order with Stack
Same idea as recursive in‑order, but use an explicit stack to traverse. Pop until leftmost, then process nodes and push right children.
Complexity Analysis
- Time: O(n) — each node visited once
- Space: O(n) worst‑case recursion stack or explicit stack
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Common Mistakes
- Forgetting to use strict inequalities (
<
and>
), allowing equal values. - Reusing a single global
prev
without resetting between test cases. - Using inclusive bounds (e.g.
low ≤ v ≤ high
) instead of open bounds. - Not handling extreme integer values when using
Integer.MIN_VALUE
/MAX_VALUE
.
Edge Cases
- Single‑node tree → always valid
- Tree with duplicate values → invalid
- Deep unbalanced tree → watch recursion depth
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
(69,299 learners)
New
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
(1,107 learners)
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
(26,683 learners)
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.