Validate Binary Search Tree

Medium Solved

Description

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

A valid BST is defined as:

  • The left subtree of a node contains only nodes with values less than the node’s value.
  • The right subtree of a node contains only nodes with values greater than the node’s value.
  • Both left and right subtrees must also be valid BSTs.

Input format / Clarification:

The tree is provided as a single JSON array representing level-order traversal, where null indicates missing nodes.

Examples

Input:

[2,1,3]

Output: true

Input:

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

Output: false

Explanation:

In the second example, node 3 is in the right subtree of 5 but is smaller than 5, which violates BST rules.

Note:

Your program must print either true or false exactly, so it can be compared with the expected output.

No submissions yet.

Discuss in-order traversal vs recursive min/max range validation approaches.

Test Cases