题目:

题解:
class Solution:
def recoverTree(self, root: Optional[TreeNode]) -> None:
"""
Do not return anything, modify root in-place instead.
"""
def inorder(node, lst):
if not node:
return
inorder(node.left, lst)
lst.append(node)
inorder(node.right, lst)
list = []
inorder(root, list)
n = len(list)
a, b = None, None
for i in range(n - 1):
if list[i].val > list[i + 1].val:
b = list[i + 1]
if a is None:
a = list[i]
elif a is not None:
break
t = a.val
a.val = b.val
b.val = t