50. Validate Binary Search Tree

Medium · Binary Tree

Given a binary tree, determine if it is a valid binary search tree (BST). A valid BST must satisfy the following properties: for every node, all values in its left subtree must be strictly less than the node's value, and all values in its right subtree must be strictly greater than the node's value. This constraint applies recursively to all subtrees.

The tree is encoded as a level-order array where each element is either a number (node value) or null (missing node). For example, [2, 1, 3] represents a tree with root 2, left child 1, and right child 3.

Examples

Example 1
Input: [2, 1, 3]
Output: true
Explanation: Root is 2, left subtree has 1 (< 2), right subtree has 3 (> 2). This is a valid BST.
Example 2
Input: [5, 1, 4, null, null, 3, 6]
Output: false
Explanation: Root is 5, right child is 4 (< 5), which violates the BST property. Even though 4's children (3 and 6) might seem valid locally, the node 4 itself is in the right subtree of 5 and must be > 5.

Constraints