099. Recover Binary Search Tree Solution 1 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def recoverTree(self, root): """ :type root: TreeNode :rtype: void Do not return anything, modify root in-place instead. """ self.node1, self.node2, self.prev = None, None, None self._traversal(root) self.node1.val, self.node2.val = self.node2.val, self.node1.val def _traversal(self, curr): if not curr: return if curr.left: self._traversal(curr.left) if self.prev: if curr.val < self.prev.val: if not self.node1: self.node1 = self.prev self.node2 = curr else: self.node2 = curr self.prev = curr if curr.right: self._traversal(curr.right)