ExamAdda Logo

Validate Binary Search Tree

Easy

Given the root of a binary tree, determine whether it is a valid Binary Search Tree (BST).

A valid BST must satisfy:

Left Subtree < Root < Right Subtree

 

And this rule must be true for every node, not just its immediate children.

Example

  2      

 / \      

1   3

 

This is a valid BST because:

1 < 2 < 3

 

But:

        5    

       / \      

     1   4     

         / \    

        3   6

 

is not a valid BST because 3 is in the right subtree of 5, but:

3 < 5

Example 1

Input

root = [2,1,3]

Output

true

Explanation

The tree is:   

       2
      / \
     1   3

For the root:

1 < 2 < 3

 

The left subtree contains only values smaller than 2, and the right subtree contains only values greater than 2.

Therefore, it is a valid BST.

Example 2

Input

root = [5,1,4,null,null,3,6]

Output

false

Explanation

The tree is:

       5
      / \
     1   4
        / \
       3   6

 At first glance, 3 < 4 < 6, so the subtree rooted at 4 looks valid.

But 4 is in the right subtree of 5.

Therefore, every node in that subtree must be greater than 5.

But:

3 < 5

 

So the tree is not a valid BST.

Constraints

1 <= number of nodes <= 10^4
-2^31 <= Node.val <= 2^31 - 1

Hints:

Hint 1

Don't check only:

left child < root < right child

 

That is not enough.

Hint 2

Every node must obey a range of allowed values.

For example:

       5
      / \
     3   7

For the left subtree:

-∞ < value < 5

 

For the right subtree:

5 < value < +∞

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 13, 2026

Last updated

Aug 14, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output