Validate Binary Search Tree
Given the root of a binary tree, determine whether it is a valid binary search tree: every node's value must be strictly greater than all values in its left subtree and strictly less than all values in its right subtree.
Open official problem prompt ↗Decide if the tree obeys the global BST ordering rule, not just local parent-child comparisons.
Like checking a nested set of number ranges: every room you enter narrows the allowed values, and each item inside must fit the room's current label.
- Input
- root = [2, 1, 3]
- Output
- true
- Why
- 1 < 2 in the left subtree and 3 > 2 in the right subtree, so BST order holds everywhere.
The number of nodes is in the range [1, 10^4]-2^31 <= Node.val <= 2^31 - 1