Validate a Binary Search Tree
Last updated: October 30, 2025
Quick Overview
Given the root of a binary tree, determine if it is a valid binary search tree where every node's left subtree contains only values less than the node, and every right subtree contains only values greater.
Intuit
October 30, 202513
8
4,759 solved
Given the root of a binary tree, determine if it is a valid binary search tree where every node's left subtree contains only values less than the node, and every right subtree contains only values greater.
Appears on Glider assessments. Tests recursive thinking and the ability to handle subtle invariants. The common mistake of only checking immediate children reveals depth of understanding.
What the Interviewer Expects
- Implement using range-based validation with min/max bounds
- Avoid the common mistake of only checking immediate parent-child relationships
- Handle edge cases including single-node trees and integer overflow boundaries
- Write clean recursive code with clear parameter naming
- Discuss iterative alternatives using in-order traversal
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What is wrong with only checking if left.val < node.val < right.val?
- How would you find the first invalid node in an invalid BST?
- Can you solve this iteratively using in-order traversal?
- How would you handle duplicate values?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
To determine if a binary tree is a valid binary search tree (BST), we need to ensure that for every node, all values in its left subtree are less than the node's value, and all values in its right sub...
Approach
- Start with the root node and initialize the min and max bounds as negative and positive infinity, respectively.
- At each node, check if its value lies within the min and max bounds.
- If it ...