Given the root of a binary search tree in which exactly two nodes were swapped by mistake, restore the tree without changing its structure. Pattern focus: Inorder Gives Sorted Sequence. A valid BST has sorted inorder traversal, so the two misplaced nodes can be identified from the violations in that sequence.
root = binary tree root with exactly two swapped values
corrected BST root
Example 1:
Input:
root = [3,1,4,null,null,2]
Output:
[2,1,4,null,null,3]
Explanation:
The values 2 and 3 are swapped; restoring them gives a valid BST.
Example 2:
Input:
root = [2,3,1]
Output:
[2,1,3]
Explanation:
The left and right child values are swapped.