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
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
  1. Start with the root node and initialize the min and max bounds as negative and positive infinity, respectively.
  2. At each node, check if its value lies within the min and max bounds.
    • If it ...

Submit Your Answer
Markdown supported

Related Questions