← DiffPush

Validate Binary Search Tree

Standard Bar Binary Search Trees · Practice Problems O(n) · O(h)

The Range-Pass Audit of the Sort Racks

A warehouse's sort racks claim the strict smaller-left, larger-right layout, but a recent retrofit may have wedged crates into the wrong aisles. The auditor walks each station carrying a permissible value window: entering a left aisle tightens the ceiling, entering a right aisle raises the floor. Any crate outside its inherited window fails the audit on the spot.

Input: A binary tree as a level-order array (null marks a missing child) — not guaranteed to be a BST.

Output: Return true if the tree satisfies the BST property for every node, otherwise false. Duplicate values are not allowed.

Constraints

Examples

Example 1

Input: {"tree":[5,1,4,null,null,3,6]}
Output: false
Node 4 sits in the right wing of 5 but holds a smaller value — the window check rejects it.

Example 2

Input: {"tree":[2,1,3]}
Output: true
Every node stays inside the window its ancestors carved out.

Solve this in your browser →

Also on LeetCode ↗