Skip to interview prep
Apexed

57. Validate a Binary Search Tree

MediumTrees

Return true if the tree is a valid BST: every node's left subtree holds strictly smaller values and its right subtree strictly larger values.

A binary tree is passed as TreeNode objects with val, left and right. The tests write trees in level order, with null for a missing child.

Example 1

Input: root = [5, 1, 4, null, null, 3, 6]
Output: false
Explanation: 4 is in 5's right subtree but smaller than 5

Constraints

  • 1 ≤ nodes ≤ 10⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Validate a Binary Search Tree — coding interview practice — Apexed