Back to Practice
#0239
Validate Binary Search Tree
MediumDSA28 min20 XP
Problem
A tree node is [value, left, right]. Return True if every node is strictly greater than all values in its left subtree and strictly less than all values in its right subtree.
Why This Matters
BST validation tests whether you understand that each node has a full allowed range, not only a parent comparison.
Function Signature
def is_valid_bst(root):
Examples
Example 1
Inputroot = [5, [1, None, None], [4, [3, None, None], [6, None, None]]]
OutputFalse
The value 4 is in the right subtree of 5, so it must be greater than 5, but it is not.
Constraints
- Return the exact requested value.
- Handle empty or small inputs when the prompt allows them.
- Prefer the target DSA pattern over brute force when the input can grow.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Invalid deep value
Input[5, [1, None, None], [4, [3, None, None], [6, None, None]]]
ExpectedFalse
A local child check would miss the full ancestor range.
Valid tree
Input[8, [3, [1, None, None], [6, None, None]], [10, None, [14, None, None]]]
ExpectedTrue
Every subtree stays inside its allowed range.
Hidden Test Categories
duplicate values are invalidempty treeinvalid value several levels deep