Two nodes of the BST are swapped by mistake. Recover the tree without changing its structure.
Inorder traversal of BST should give sorted sequence. Find two nodes where order is violated: first is the node where nums[i] > nums[i+1] for the first time, second is the smaller of the violating pair or the last violation.
- Do inorder traversal, tracking prev node.
- When prev.val > cur.val: if first==null, first=prev; second=cur.
- After traversal, swap first.val and second.val.
- Time Complexity: O(N)
- Space Complexity: O(H)