My first instinct was to just do the standard recursive validation with a low/high range per node, which is right, but I kept wanting to return a boolean and had to mentally reset.
Use a recursive in-order traversal that passes down the valid (min, max) range for each node. At each node, check if its value falls within the range; if not, increment a counter. Then recurse left with updated max and right with updated min, ensuring the range constraints are enforced.
Pro tip: Clarify whether the BST property allows duplicates and how to handle them (e.g., strict inequality). Also, mention that you can combine the validation and counting in one pass to achieve O(n) time and O(h) space.
Ask about duplicate values, null nodes, and whether the tree is guaranteed to be binary. Confirm that 'violate' means the node's value is outside its valid range.
Design a helper function that takes a node, a lower bound, and an upper bound, and returns the count of violations in the subtree. Use a nonlocal counter or return the count.
At each node, if its value is not within (lower, upper), increment the violation count. Then recursively process left child with upper bound = node.val and right child with lower bound = node.val.
If the node is null, return 0 (or the current count). After traversing, return the total count of violations.
State that time complexity is O(n) and space is O(h) due to recursion stack. Walk through a small example to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.