Validate Binary Search Tree
Problem Statement
You are given the root of a binary tree. Decide whether it is a valid binary search tree.
Every node must be strictly bigger than every value in its left subtree, and strictly smaller than every value in its right subtree. The rule covers the whole subtree, not only the direct children. Equal values are not allowed.
The root of a binary tree (between 1 and 10^4 nodes). Values can be any 32-bit int, including the smallest and largest int.
true if the tree is a valid binary search tree, otherwise false.
1 is smaller than 2, and 3 is bigger than 2.
The root is 5, but its right child is 4, which is not bigger than 5.
- • The number of nodes is between 1 and 10^4
- • −2^31 ≤ Node.val ≤ 2^31 − 1
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For every node, scan its left subtree for the max and its right subtree for the min
Write the inorder walk into a list, then check that the list strictly increases
Each node must sit strictly between a low and a high bound set by its ancestors
For each node, find the biggest value in its left subtree and the smallest value in its right subtree, and check both against the node. Do this for every node. Each check scans a whole subtree, so the same values are scanned again and again.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.