<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 {
public:
    void swap(TreeNode* node1, TreeNode* node2){
        int temp = node1->val;
        node1->val = node2->val;
        node2->val = temp;
    }
    void inorder(TreeNode* root,vector<int> &arr){
        if(root==nullptr){
            return;
        }
        inorder(root->left,arr);
        arr.push_back(root->val);
        inorder(root->right,arr);
    }
    void call(TreeNode* root,int a, int b,TreeNode* &first,TreeNode* &second,bool &flag){
        if(root==nullptr){
            return;
        }
        if(root->val == a || root->val == b){
            if(flag==true){
                second = root;
                swap(first,second);
                return;
            }
            first = root;
            flag = true;
        }
        if(root->left){
            call(root->left,a,b,first,second,flag);
        }
        if(root->right){
            call(root->right,a,b,first,second,flag);
        }
    }
    void recoverTree(TreeNode* root) {
        vector<int> arr;
        int a=0,b=0;
        inorder(root,arr);
        TreeNode*first;
        TreeNode* second;
        bool flag=false;
        vector<int> temp;
        for(int i:arr){
            temp.push_back(i);
        }
        sort(temp.begin(),temp.end());
        for(int i=0;i<arr.size();i++){
            if(arr[i] == temp[i]){
                continue;
            }
            else{
                if(a!=0){
                    b=arr[i];
                    continue;
                }
                a=arr[i];
                continue;
            }
        }
        call(root,a,b,first,second,flag);
    }
};
61600