It's basically a reversed in-order traversal.
Use an iterative in-order traversal (left, root, right) to visit nodes in ascending order, but stop after visiting (n - k + 1) nodes to find the k-th largest. Alternatively, perform a reverse in-order traversal (right, root, left) and stop at the k-th visited node. Both approaches use an explicit stack to avoid recursion.
Pro tip: Clarify whether k is 1-indexed and whether the tree can be modified; then mention that reverse in-order is more efficient because it stops early without needing to know the total number of nodes. Also, discuss edge cases like k > number of nodes or k <= 0.
Confirm that k is 1-indexed, the tree is a BST, and the solution must be iterative. Discuss edge cases such as k being larger than the number of nodes or k <= 0.
Decide between in-order (ascending) or reverse in-order (descending). Reverse in-order is preferred because it directly finds the k-th largest without needing the total node count.
Use a stack to simulate recursion. For reverse in-order, push all right children, then process the node, then move to the left child. Keep a counter to track visited nodes.
Once the counter reaches k, return the current node's value. If traversal completes without reaching k, handle the invalid case (e.g., return -1 or throw an exception).
State time complexity O(h + k) where h is tree height, and space complexity O(h). 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.