<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?
class Solution:
    # https://www.bilibili.com/video/BV17H4y1p7DD/?spm_id_from=333.337.search-card.all.click&vd_source=e0270d24dfbd34b0ea0d64dce9e088b3

#     The idea of **Morris** algorithm is to **set the temporary link between the node and its
# predecessor**: predecessor.right = root. So one starts from the node, computes its predecessor and verifies if the link is present.
#     1. There is no link? Set it and go to the left subtree.
#     3. There is a link? Break it and go to the right subtree.
#     5. There is one small issue to deal with : what if there is no left child, i.e. there is no left subtree? Then go straightforward to the right subtree.

    def recoverTree(self, root: TreeNode) -> None: 
        # pred is a 'true' predecessor, the previous node in the inorder traversal.
        x = y = prev = None

        while root: 
            if not root.left: # no left child, then just go right subtree.
                # Check for the swapped nodes
                if prev and root.val < prev.val:
                    y = root
                    if x is None:
                        x = prev
                prev = root 
                root = root.right 
            else:  # If there is a left child, find the predecessor. 
                predecessor = root.left
                while predecessor.right and predecessor.right != root: # note the extra check: `predecessor.right != root`
                    predecessor = predecessor.right 
                # Set the link `predecessor.right = root` **temporarily** and explore left subtree
                if predecessor.right is None:
                    predecessor.right = root
                    root = root.left 
                else: # Delete the link and go right  
                    if prev and root.val < prev.val:
                        y = root
                        if x is None:
                            x = prev
                    prev = root 
                    predecessor.right = None
                    root = root.right
            

        x.val, y.val = y.val, x.val
16600