<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.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
    // int stepValue = INT_MIN;

    vector<TreeNode*> valList;

public:
    void recoverTree(TreeNode* root) {

        // is the two node have to swap is adjacent? I think not.
        // how many nodes are in the tree? 2 <= node.size()  <= 1000
        // could you pls clarify the value range of the node value?

        // first, it equals to the minimum value,
        // and I expected it would be increase forever, it invalid, means we
        // find the node has to swap value.

        traverse(root);

        // debug
        // for (int i = 0; i < valList.size(); i++) {
        //     cout << valList[i]->val;
        //     if (i != valList.size() - 1)
        //         cout << ", ";
        // }

        // two scenario
        // If there is 1 value invalid, that means it only has to swap with it's
        // adjacent But if there are 2 value invalid, we will swap with the two
        // node.

        int stepValue = valList[0]->val;

        vector<int> swapIndex;
        for (int i = 1; i < valList.size(); i++) {
            if (valList[i]->val < stepValue) {
                swapIndex.push_back(i);
                stepValue = valList[i]->val;
            } else {
                stepValue = valList[i]->val;
            }
        }

        // cout << "swapIndex.size():"<<swapIndex.size()<<endl;

        if (swapIndex.size() == 1) {
            swap(valList[swapIndex[0]], valList[swapIndex[0] - 1]);
        } else if (swapIndex.size() == 2) {
            // if there are two inversions, first node will be the previous one in the current inversions,
            // and the second one will be the current inversions.
            swap(valList[swapIndex[0]-1], valList[swapIndex[1]]);
        } else {
            cout << "will not be here";
        }

        return;
    }

    // in-order traversal
    void traverse(TreeNode* root) {
        // base case
        if (root == nullptr) {
            return;
        }

        // general case
        traverse(root->left);

        // so something at the middle
        valList.push_back(root);

        traverse(root->right);
    }

    // the element that is invalid is in valList.size()-1
    // find which node should swap with it.

    // valList.size()-1 swap with  valList.size()-2

    // 1..3...5..7...9...13....8
    // 1..3...5..7...13....8

    // 3,2,1

    void swap(TreeNode* first, TreeNode* second) {
        int tmp = first->val;
        first->val = second->val;
        second->val = tmp;
    }
};
61300