Back to problems Sign in to unlock
Leetcode
Medium
Trees
Recursion
Binary Search
Google
Amazon
Microsoft
Validate Binary Search Tree
Determine whether a binary tree satisfies the binary search tree ordering rules.
Acceptance 0%
Problem Statement
Given the root of a binary tree, determine whether it is a valid binary search tree (BST).
A binary tree is a valid BST if for every node:
- all values in its left subtree are strictly smaller than the node's value
- all values in its right subtree are strictly larger than the node's value
- both subtrees are also valid BSTs
Your task is to return whether the entire tree obeys these rules.
Input Format
- A binary tree root is provided as the input.
- The tree nodes contain integer values.
- Null children indicate missing nodes.
Output Format
- Return
trueif the tree is a valid BST. - Return
falseotherwise.
Constraints
- The tree may be empty.
- Node values are integers.
- Duplicate values are not allowed in a valid BST.
- Focus on correctness for all subtree boundary constraints, not just immediate parent-child comparisons.
Examples
Sample cases returned by the problem API.
Example 1
Input
root = [2,1,3]
Output
true
Explanation
Left child 1 is smaller than 2, and right child 3 is larger than 2, so the tree is a valid BST.
Example 2
Input
root = [5,1,4,null,null,3,6]
Output
false
Explanation
The node 3 is in the right subtree of 5, but 3 is less than 5, so the tree is not a valid BST.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.