Validate Binary Search Tree
Part of Practice: Levels & BST Rules
Given the root of a binary tree, determine whether it is a valid binary search tree (BST). Valid means: every value in a node's left subtree is strictly less than the node's value, every value in its right subtree is strictly greater, and the same holds at every node. Duplicates are not allowed anywhere.
The tree has 1 to 10,000 nodes, and values span the full 32-bit integer range — so be careful if you use sentinel values as initial bounds.
Examples
- Input:
- root = [2,1,3]
- Output:
- true
- Input:
- root = [5,1,4,null,null,3,6]
- Output:
- false
The root's right child is 4, which is less than 5.
- Input:
- root = [5,4,6,null,null,3,7]
- Output:
- false
Locally everything looks fine (3 < 6, 4 < 5 < 6), but the 3 sits inside the root's right subtree while being less than 5 — the BST rule applies to entire subtrees, not just direct children.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.