My first instinct was to do an inorder traversal and scan through, which works but they clearly wanted something that uses the BST structure.
Use the BST property to traverse from the root, maintaining a candidate for the largest key smaller than the target. At each node, if the node's key is less than the target, update the candidate and move right; otherwise, move left. Continue until reaching a null node, then return the candidate.
Pro tip: Clarify edge cases upfront, such as when no such key exists or when the tree is empty, and discuss how to handle duplicates if the BST allows them. This shows thoroughness and prevents misunderstandings.
Confirm the definition of 'strictly smaller' and ask about edge cases like empty tree, no predecessor, or duplicate keys. Ensure you understand the expected return value if no such key exists.
Explain that in a BST, for any node, all keys in the left subtree are smaller and all keys in the right subtree are larger. This allows efficient search.
Start at the root with a candidate variable (e.g., null). While the current node is not null, compare its key with the target: if less, update candidate and move right; if greater or equal, move left.
State that the time complexity is O(h) where h is the height of the tree, and space complexity is O(1) for the iterative version. Mention that in a balanced BST, this is O(log n).
Discuss what to return if no such key exists (e.g., null or -1). Optionally, mention a recursive alternative and compare trade-offs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.