← All Exercises

Validate a Binary Search Tree

intermediatejavascript~25min

Background

BST validation guards the correctness of ordered maps and is a frequent interview check for boundary handling.

Problem

Given a binary tree encoded as a level-order array where null marks a missing node, determine whether it satisfies the binary search tree property.

Input

An array of numbers and nulls in level-order encoding; children of index i sit at 2i+1 and 2i+2.

Output

A boolean: true when every node lies within its allowed open interval.

Sample tests

Sample 1

Input: values = [5,3,8,null,null,7,9]
Expected: true

Sample 2

Input: values = [5,1,4,null,null,3,6]
Expected: false

Sample 3

Input: values = [2,1,3]
Expected: true

Your solution

Learning objective

Propagate strict lower and upper bounds through a tree recursion instead of checking local parent-child order.