LeetCode – Recover Binary Search Tree (Java)

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure.

Java Solution

Inorder traveral will return values in an increasing order. So if an element is less than its previous element,the previous element is a swapped node.

public class Solution {
    TreeNode first;
    TreeNode second; 
    TreeNode pre; 
 
    public void inorder(TreeNode root){
        if(root==null)
            return;
 
        inorder(root.left);
 
        if(pre==null){
            pre=root;
        }else{
            if(root.val<pre.val){
                if(first==null){
                    first=pre;
                }
 
                second=root;
            }
            pre=root;
        }
 
        inorder(root.right);
    }
 
    public void recoverTree(TreeNode root) {
        if(root==null)
            return;
 
        inorder(root);
        if(second!=null && first !=null){
            int val = second.val;
            second.val = first.val;
            first.val = val;
        }
 
    }
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>