# Two nodes of a BST are swapped, correct the BST | Set-2

Given a Binary Search Tree with two of the nodes of the Binary Search Tree (BST) swapped. The task is to fix (or correct) the BST.

Note: The BST will not have duplicates.

Examples:

```Input Tree:
10
/  \
5    8
/ \
2   20

In the above tree, nodes 20 and 8 must be swapped to fix the tree.
Following is the output tree
10
/  \
5    20
/ \
2   8
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :

• Traverse the BST in In-order fashion and store the nodes in a vector.
• Then this vector is sorted after creating a copy of it.
• Use Insertion sort as it works the best and fastest when the array is almost sorted. As in this problem, only two elements will be displaced so Insertion sort here will work in linear time.
• After sorting, compare the sorted vector and the copy of the vector created earlier, by this, find out the error-some nodes and fix them by finding them in the tree and exchanging them.

Below is the implementation of above approach:

 `// C++ implementation of the above approach ` `#include ` `using` `namespace` `std; ` `  `  `// A binary tree node has data, pointer  ` `// to left child and a pointer to right child  ` `struct` `node { ` `    ``int` `data; ` `    ``struct` `node *left, *right; ` `    ``node(``int` `x) ` `    ``{ ` `        ``data = x; ` `        ``left = right = NULL; ` `    ``} ` `}; ` `  `  `// Utility function for insertion sort ` `void` `insertionSort(vector<``int``>& v, ``int` `n) ` `{ ` `    ``int` `i, key, j; ` `    ``for` `(i = 1; i < n; i++) { ` `        ``key = v[i]; ` `        ``j = i - 1; ` `        ``while` `(j >= 0 && v[j] > key) { ` `            ``v[j + 1] = v[j]; ` `            ``j = j - 1; ` `        ``} ` `        ``v[j + 1] = key; ` `    ``} ` `} ` `  `  `// Utility function to create a vector  ` `// with inorder traversal of a binary tree ` `void` `inorder(node* root, vector<``int``>& v) ` `{ ` `    ``// Base cases ` `    ``if` `(!root) ` `        ``return``; ` `  `  `    ``// Recurive call for left subtree ` `    ``inorder(root->left, v); ` `  `  `    ``// Insert node into vector ` `    ``v.push_back(root->data); ` `  `  `    ``// Recursive call for right subtree ` `    ``inorder(root->right, v); ` `} ` `  `  `// Function to exchange data  ` `// for incorrect nodes ` `void` `find(node* root, ``int` `res, ``int` `res2) ` `{ ` `    ``// Base cases ` `    ``if` `(!root) { ` `        ``return``; ` `    ``} ` `  `  `    ``// Recurive call to find  ` `    ``// the node in left subtree ` `    ``find(root->left, res, res2); ` `  `  `    ``// Check if current node  ` `    ``// is incorrect and exchange ` `    ``if` `(root->data == res) { ` `        ``root->data = res2; ` `    ``} ` `    ``else` `if` `(root->data == res2) { ` `        ``root->data = res; ` `    ``} ` `  `  `    ``// Recurive call to find  ` `    ``// the node in right subtree ` `    ``find(root->right, res, res2); ` `} ` `  `  `// Primary function to fix the two nodes ` `struct` `node* correctBST(``struct` `node* root) ` `{ ` `    ``// Vector to store the  ` `    ``// inorder traversal of tree ` `    ``vector<``int``> v; ` `  `  `    ``// Function call to insert ` `    ``// nodes into vector ` `    ``inorder(root, v); ` `  `  `    ``// create a copy of the vector ` `    ``vector<``int``> v1 = v; ` `  `  `    ``// Sort the original vector thereby  ` `    ``// making it a valid BST's inorder ` `    ``insertionSort(v, v.size()); ` `  `  `    ``// Traverse through both vectors and ` `    ``// compare misplaced values in original BST ` `    ``for` `(``int` `i = 0; i < v.size(); i++) { ` ` `  `        ``// Find the mismatched values ` `        ``// and interchange them ` `        ``if` `(v[i] != v1[i]) { ` ` `  `            ``// Find and exchange the  ` `            ``// data of the two nodes ` `            ``find(root, v1[i], v[i]); ` `  `  `            ``// As it given only two values are  ` `            ``// wrong we don't need to check further ` `            ``break``; ` `        ``} ` `    ``} ` `  `  `    ``// Return the root of corrected BST ` `    ``return` `root; ` `} ` ` `  `// A utility function to ` `// print Inoder traversal  ` `void` `printInorder(``struct` `node* node) ` `{ ` `    ``if` `(node == NULL) ` `        ``return``; ` `    ``printInorder(node->left); ` `    ``printf``(``"%d "``, node->data); ` `    ``printInorder(node->right); ` `} ` `  `  `int` `main() ` `{  ` `    ``struct` `node* root = ``new` `node(6); ` `    ``root->left = ``new` `node(10); ` `    ``root->right = ``new` `node(2); ` `    ``root->left->left = ``new` `node(1); ` `    ``root->left->right = ``new` `node(3); ` `    ``root->right->right = ``new` `node(12); ` `    ``root->right->left = ``new` `node(7); ` `  `  `    ``printf``("Inorder Traversal of the ` `                           ``original tree \n"); ` ` `  `    ``printInorder(root); ` `  `  `    ``correctBST(root); ` `  `  `    ``printf``("\nInorder Traversal of the  ` `                              ``fixed tree \n"); ` ` `  `    ``printInorder(root); ` `  `  `    ``return` `0; ` `} `

Time Complexity: O(N)
Auxiliary Space: O(N)
where N is the number of nodes in the Binary Tree.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai