Went with in-order traversal pretty quickly since BST in-order gives you sorted order.
Use an in-order traversal of the BST to visit nodes in ascending order, counting until you reach the k-th node. For efficiency, implement an iterative traversal with a stack to avoid recursion overhead and allow early termination. Explicitly handle edge cases: if the tree is empty or k is less than 1 or greater than the number of nodes, return an appropriate error or sentinel value.
Pro tip: Mention that you can optimize by augmenting each node with the size of its left subtree, enabling O(log n) search for the k-th smallest in a balanced BST. Also, clarify with the interviewer what to return for invalid k (e.g., null, -1, or throw an exception) to align with expectations.
Ask the interviewer about the expected return value for invalid inputs (empty tree, k out of bounds) and whether k is 1-indexed. Confirm if the tree can be modified or if additional data structures are allowed.
Decide between recursive and iterative in-order traversal. Iterative is preferred for early termination and avoiding stack overflow; recursive is simpler but may traverse the entire tree.
Traverse the tree in-order, incrementing a counter for each visited node. When the counter equals k, return the current node's value. If traversal completes without reaching k, handle the out-of-bounds case.
Check if the root is null and return the agreed-upon value. Validate k: if k <= 0 or k > total nodes, return the agreed-upon error value. Optionally, compute total nodes first if needed.
State time complexity O(h + k) for iterative in-order (h is height) and space O(h). Mention that augmenting nodes with subtree sizes can achieve O(log n) for balanced trees, but requires extra space and maintenance.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.