Problem: Recover Binary Search Tree
Explore how to recover a binary search tree when two nodes are swapped by mistake. Learn to implement a constant space solution using Morris inorder traversal, detect violations in the inorder sequence, and swap nodes to restore the tree. This lesson helps you apply advanced tree traversal techniques and understand space-efficient algorithms in C#.
We'll cover the following...
We'll cover the following...
Statement
You are given the root of a binary search tree (BST) in which the values of exactly two nodes have been swapped by mistake. Restore the BST to its correct form without altering the structure of the tree.
Note: A solution using
space is relatively straightforward. Can you devise a constant space solution?
Constraints:
The number of nodes in the tree is in the range
...