Two nodes of a BST are swapped , correct the BST | Set | Java implementation of the above approach ; A binary tree node has data , pointer to left child and a pointer to right child ; Utility function for insertion sort ; Utility function to create a vector with inorder traversal of a binary tree ; Base cases ; Recursive call for left subtree ; Insert node into vector ; Recursive call for right subtree ; Function to exchange data for incorrect nodes ; Base cases ; Recursive call to find the node in left subtree ; Check if current node is incorrect and exchange ; Recursive call to find the node in right subtree ; Primary function to fix the two nodes ; Vector to store the inorder traversal of tree ; Function call to insert nodes into vector ; create a copy of the vector ; Sort the original vector thereby making it a valid BST 's inorder ; Traverse through both vectors and compare misplaced values in original BST ; Find the mismatched values and interchange them ; Find and exchange the data of the two nodes ; As it given only two values are wrong we don 't need to check further ; Return the root of corrected BST ; A utility function to print Inorder traversal ; Driver code
import java . util . ArrayList ; class GFG { static class node { int data ; node left , right ; public node ( int x ) { data = x ; left = right = null ; } } ; static void insertionSort ( ArrayList < Integer > v , int n ) { int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = v . get ( i ) ; j = i - 1 ; while ( j >= 0 && v . get ( j ) > key ) { v . set ( j + 1 , v . get ( i ) ) ; j = j - 1 ; } v . set ( j + 1 , key ) ; } } static void inorder ( node root , ArrayList < Integer > v ) { if ( root == null ) return ; inorder ( root . left , v ) ; v . add ( root . data ) ; inorder ( root . right , v ) ; } static void find ( node root , int res , int res2 ) { if ( root == null ) { return ; } find ( root . left , res , res2 ) ; if ( root . data == res ) { root . data = res2 ; } else if ( root . data == res2 ) { root . data = res ; } find ( root . right , res , res2 ) ; } static node correctBST ( node root ) { ArrayList < Integer > v = new ArrayList < > ( ) ; inorder ( root , v ) ; ArrayList < Integer > v1 = new ArrayList < > ( v ) ; insertionSort ( v , v . size ( ) ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v . get ( i ) != v1 . get ( i ) ) { find ( root , v1 . get ( i ) , v . get ( i ) ) ; break ; } } return root ; } static void printInorder ( node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . printf ( " % d ▁ " , node . data ) ; printInorder ( node . right ) ; } public static void main ( String [ ] args ) { 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 ) ; System . out . printf ( " Inorder ▁ Traversal ▁ of ▁ the " ) ; System . out . printf ( "original tree NEW_LINE"); printInorder ( root ) ; correctBST ( root ) ; System . out . printf ( " Inorder Traversal of the "); System . out . printf ( "fixed tree NEW_LINE"); printInorder ( root ) ; } }