Skip to main content
Advancedcoding Verified

Two nodes of a binary search tree were swapped by mistake. Find them and fix the tree without changing its structure.

ReportedSource: GeeksforGeeks interview experience: Microsoft SDE-II, level 61

Quick answerFree

In-order traversal; record the first prev where prev > cur and the last cur where prev > cur; swap their values. O(n), O(h) (O(1) with Morris traversal).

Detailed answer· ~110 words

Pro

Unlock the full answer with Pro

Get a ~110-word model answer and a runnable reference solution, plus 82 more DSA questions.

Reference solution· runs in the browser sandbox

Pro
Unlock with Pro

+ 82 more DSA questions in Pro, each with the full answer.

Discussion