Validate a Binary Search Tree
MediumTrees
Decode it first. Clues, constraints, pattern and plan before any code.Given the root of a binary tree, return whether it is a valid binary search tree (BST). In a valid BST, for every node:
- every value in its left subtree is strictly less than the node's value, and
- every value in its right subtree is strictly greater.
Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them.
[2, 1, 3] → True
[5, 1, 4, null, null, 3, 6] → False (4 is in 5's right subtree but is smaller)
[5, 4, 6, null, null, 3, 7] → False (3 is below 6, which is fine, but it is also in 5's right subtree)An empty tree counts as valid.
Constraints
0 <= number of nodes <= 10^4-2^31 <= Node.val <= 2^31 - 1