My first instinct was to try to exploit the BST property and do something clever with the ordering, which was a mistake because the whole premise is that the invariants are broken.
First, clarify that the BST-like ordering is irrelevant for finding the mode, so we can ignore corruption and simply traverse the entire tree to count frequencies. Then, use a hash map to tally each node's value during a full traversal, and finally scan the map to find the most frequent value(s).
Pro tip: Mention that the mode can be multi-valued and discuss tie-breaking or returning all modes; also note that the tree's corruption doesn't affect the algorithm, showing you can separate irrelevant constraints from the core problem.
Ask whether the mode should be a single value or all values with maximum frequency, and confirm that the tree may be corrupted but we still need to traverse all nodes. Also discuss input size to choose an efficient approach.
Select a tree traversal (e.g., DFS or BFS) that visits every node exactly once. Since order doesn't matter for counting, any traversal works; iterative DFS avoids recursion depth issues.
During traversal, use a hash map (dictionary) to map each node value to its frequency. Update the count for each visited node.
Iterate through the hash map to find the maximum frequency and collect all values that achieve it. Return the mode(s) as required.
State time complexity O(n) and space complexity O(n) for the hash map (plus traversal stack/queue). Discuss edge cases: empty tree, all unique values, multiple modes, and large trees.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.