1
# Runtime: 61 ms (Top 95.6%) | Memory: 16.68 MB (Top 75.1%)6
Brute force kind of thing7
-> Inorder Traversal returns sorted array8
-> find a swap btwn numbers to make sorted9
Make single swap to make array sorted10
[1, 2, 3, 4, 10, 6, 9, 5, 10, 12]12
prev number is mismatch -> 10 is cause13
now go frm right to left14
[1, 2, 3, 4, 10, 6, 9, 5, 11, 12]16
mismatch with next number -> 5 is the cause21
x No -> 3 is the cause23
x No -> 1 is the cause27
def inorder(self, root, li):30
li = self.inorder(root.left, li)32
li = self.inorder(root.right, li)35
def recoverTree(self, root: TreeNode) -> None:37
Do not return anything, modify root in-place instead.39
li = self.inorder(root, [])44
if li[i].val < li[i - 1].val:48
for i in range(n - 2, -1, -1):49
if li[i].val > li[i + 1].val:53
a.val, b.val = b.val, a.val