2Brute2Best
🔥0

Validate Binary Search Tree

Medium·LeetCode #98 Pro OnlyBasic Reinforcement
LeetCode
01

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.

Input

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.

Output

true if the tree is a valid binary search tree, otherwise false.

Example 1
Input: root = [2, 1, 3]
Output: true

1 is smaller than 2, and 3 is bigger than 2.

Example 2
Input: root = [5, 1, 4, null, null, 3, 6]
Output: false

The root is 5, but its right child is 4, which is not bigger than 5.

Constraints
  • • The number of nodes is between 1 and 10^4
  • • −2^31 ≤ Node.val ≤ 2^31 − 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“left < node < right”
→ A rule for each node that covers its whole subtree, not only the children.
“strictly bigger / strictly smaller”
→ Equal values fail, so both bounds are strict.
“is this tree valid”
→ One bad node anywhere makes the answer false.
03

Approach Complexity Ladder

Brute Force — scan each node's whole subtreesO(N²)O(H)

For every node, scan its left subtree for the max and its right subtree for the min

Better — the inorder values must strictly increaseO(N)O(N)

Write the inorder walk into a list, then check that the list strictly increases

Optimal — pass a (low, high) range down the treeO(N)O(H)

Each node must sit strictly between a low and a high bound set by its ancestors

Brute Force Intuition

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:

Left < node < right. Inorder is sorted; a valid range (low, high) is passed down.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.