You are given the root of a binary tree. A valid Binary Search Tree (BST) must satisfy: for every node with value v, all values in its left subtree are strictly < v, and all values in its right subtree are strictly > v (i.e., the constraint is with respect to all ancestors, not just the parent).
Instead of returning a boolean, return the number of nodes that violate the BST rule.
(low, high)
range implied by ancestors).
n
up to ~
10^5
.
O(n)
time.