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
Coding & Algorithms
Software Engineer
Intuit
October 30, 2025
Software Engineer
Online Assessment (Glider)
Coding & Algorithms
Medium

13

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
Binary Search Tree
Tree Traversal
Recursion
Invariant Checking
In-Order Traversal
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Approach

Use a recursive helper that passes valid range bounds (min_val, max_val) down the tree. For each node, verify its value falls within the allowed range...

Implementation

Define is_valid(node, min_val, max_val). Base case: if node is None, return True. Check: if node.val <= min_val or node.val >= max_val, return False. ...


Submit Your Answer
Markdown supported

Related Questions