# Description

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

The original problem is here.

The original code is here.

# My Solution

I solve this problem in C++, as below:

``````/*
*Recover Binary Search Tree
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
*/
#include<iostream>
#include<stdlib.h>
using namespace std;

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void recoverTree(TreeNode* root) {
if(root==NULL)
return;

TreeNode *prev = NULL ;
TreeNode *curr = root;

while(curr != NULL){
if(curr->left == NULL){
WrongDetect(prev, curr);
prev = curr;
curr = curr->right;
}
else{
TreeNode * node = curr->left;
while(node->right != NULL && node->right != curr)
node = node->right;
if(node->right == NULL){
node->right = curr;
curr = curr->left;
}
else{
WrongDetect(prev, curr);
node->right = NULL;
prev = curr;
curr = curr->right;
}
}
}
}
void WrongDetect(TreeNode * prev, TreeNode * curr){
if(prev != NULL && prev->val > curr->val){