<p>You are given the <code>root</code> of a binary search tree (BST), where the values of <strong>exactly</strong> two nodes of the tree were swapped by mistake. <em>Recover the tree without changing its structure</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/28/recover1.jpg" style="width: 422px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [1,3,null,null,2]
<strong>Output:</strong> [3,1,null,null,2]
<strong>Explanation:</strong> 3 cannot be a left child of 1 because 3 &gt; 1. Swapping 1 and 3 makes the BST valid.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/28/recover2.jpg" style="width: 581px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,null,2]
<strong>Output:</strong> [2,1,4,null,null,3]
<strong>Explanation:</strong> 2 cannot be in the right subtree of 3 because 2 &lt; 3. Swapping 2 and 3 makes the BST valid.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[2, 1000]</code>.</li>
	<li><code>-2<sup>31</sup> &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> A solution using <code>O(n)</code> space is pretty straight-forward. Could you devise a constant <code>O(1)</code> space solution?
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def enumerate_nodes(self, root):
        if root.left is not None:
            root.left.id = self.latest_id
            self.node_map[self.latest_id] = root.left
            self.latest_id += 1
            self.enumerate_nodes(root.left)
        if root.right is not None:
            root.right.id = self.latest_id
            self.node_map[self.latest_id] = root.right
            self.latest_id += 1
            self.enumerate_nodes(root.right)

    # def check_graph(self, root, left_nodes=[], right_nodes=[]):
    def check_graph(self, root, min_val=None, max_val=None):
        # if any(s < root.val for s in right_nodes):
        #     return False
        # if any(s > root.val for s in left_nodes):
        #     return False
        if max_val is not None and root.val > max_val:
            return False
        if min_val is not None and root.val < min_val:
            return False
        if root.left is not None:
            if max_val is None:
                new_max_val = root.val
            else:
                new_max_val = min(max_val, root.val)
            if not self.check_graph(root.left, min_val, max_val=new_max_val):
                return False
        if root.right is not None:
            if min_val is None:
                new_min_val = root.val
            else:
                new_min_val = max(min_val, root.val)
            if not self.check_graph(root.right, new_min_val, max_val):
                return False
        return True

    def recoverTree(self, root: Optional[TreeNode]) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        root.id = 0
        self.node_map = {0: root}
        self.latest_id = 1
        self.enumerate_nodes(root)
        graph_ok = False
        for i in self.node_map:
            for j in self.node_map:
                if j == i:
                    break
                val_i, val_j = self.node_map[i].val, self.node_map[j].val
                self.node_map[i].val = val_j
                self.node_map[j].val = val_i
                graph_ok = self.check_graph(root)
                if graph_ok:
                    break
                self.node_map[i].val = val_i
                self.node_map[j].val = val_j
            if graph_ok:
                break
        assert graph_ok
16500