Example 1 - simple adjacent swap
Input
[3, 1, 2]
Output
[1, 2, 3]
The tree built from level-order [3,1,2] has nodes 3 and 2 swapped relative to a valid BST [2,1,3]. After recovery the inorder traversal is [1, 2, 3].
Full lesson preview
Two nodes in a binary search tree (BST) were swapped by mistake. Recover the BST by fixing the node values without changing the tree structure.
Problem statement
Task
Examples
Input
[3, 1, 2]
Output
[1, 2, 3]
The tree built from level-order [3,1,2] has nodes 3 and 2 swapped relative to a valid BST [2,1,3]. After recovery the inorder traversal is [1, 2, 3].
Input format
Output format
Constraints
Samples
Input
[3,1,4,None,None,2]
Output
[1, 2, 3, 4]
This corresponds to the common LeetCode example. After fixing the swapped nodes, the inorder traversal is sorted.