BST Traversal with Edge Cases
Problem Implement a traversal over a binary search tree — in-order traversal, or a BST search/validity check built on top of it — and make it correct against the edge cases probed during the round: a fully skewed tree that degenerates into a linked list, and nodes holding negative values.
Input / Output
- Input: the
rootof a BST. - Output: the node values in in-order sequence, which for a valid BST is sorted ascending — or the boolean/target result in the search variant.
Constraints
- Up to ~10^5 nodes, so a skewed tree gives recursion depth O(n) — deep enough to blow the call stack.
- Node values may be negative, zero, or span the full integer range including INT_MIN and INT_MAX.
- The tree may be empty (
root = null).
Example
- Balanced:
[2,1,3]→ in-order [1,2,3]. - Skewed: a right chain 1 → 2 → 3 → … 10^5 deep still yields [1,2,3,…], but naive recursion overflows the stack first.
- Negative values:
[-2,-3,-1]→ [-3,-2,-1]. A validity check seeded with 0 or -1 as the "no previous value" sentinel wrongly rejects this — the trap the edge case is testing.
asked …