Recover BST - Two Nodes are Swapped, Correct it

Last Updated : 18 Jul, 2026

Given the root of a Binary Search Tree (BST), where exactly two nodes have been swapped by mistake, restore the BST by swapping the values of the misplaced nodes. Return the root of the corrected BST.

Note: It is guaranteed that exactly two nodes have been swapped, and restoring their values will make the tree a valid BST. The structure of the tree must remain unchanged.

Examples:

Input: root = [6, 10, 2, 1, 3, 7, 12]

file

Output: [6, 2, 10, 1, 3, 7, 12]
Explanation: After swapping the node 10 and 2. The tree satisfies the BST property.

2

Input: root = [1,2,3]

420046840

Output: [2,1, 3]
Explanation: After swapping nodes 1 and 2, the tree becomes valid BST.

420046839
Try It Yourself
redirect icon

[Naive Approach] Using Inorder Traversal and Sorting - O(n * log n) Time and O(n) Space

The idea is to use the property of BST: an inorder traversal of a valid BST gives elements in sorted order. First, traverse the tree and store all node values in an array. Since exactly two nodes are swapped so that array will not be fully sorted. Sort the array to get the correct order of elements. Finally, traverse the tree again in inorder fashion, and replace each node’s value with the corresponding value from the sorted array. This restores the original BST structure while maintaining all other nodes in place.

C++
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

// Node structure
class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

// Print tree in level order array format
void printTree(Node* root) {
    if (root == nullptr) {
        cout << "[]\n";
        return;
    }

    vector<string> ans;
    queue<Node*> q;
    q.push(root);

    while (!q.empty()) {
        Node* curr = q.front();
        q.pop();

        if (curr != nullptr) {
            ans.push_back(to_string(curr->data));
            q.push(curr->left);
            q.push(curr->right);
        } else {
            ans.push_back("N");
        }
    }

    while (!ans.empty() && ans.back() == "N")
        ans.pop_back();

    cout << "[";
    for (int i = 0; i < ans.size(); i++) {
        if (i)
            cout << ", ";
        cout << ans[i];
    }
    cout << "]\n";
}

// Store inorder traversal
void findInorder(Node* root, vector<int>& inorder) {
    if (root == nullptr)
        return;

    findInorder(root->left, inorder);
    inorder.push_back(root->data);
    findInorder(root->right, inorder);
}

// Replace node values using sorted inorder array
void correctBSTUtil(Node* root, vector<int>& inorder, int& index) {
    if (root == nullptr)
        return;

    correctBSTUtil(root->left, inorder, index);
    root->data = inorder[index++];
    correctBSTUtil(root->right, inorder, index);
}

// Function to restore the BST
Node* correctBST(Node* root) {
    vector<int> inorder;

    findInorder(root, inorder);

    sort(inorder.begin(), inorder.end());

    int index = 0;
    correctBSTUtil(root, inorder, index);

    return root;
}

int main() {

    // Constructing the tree with swapped nodes
    //       6
    //     /  \
    //    10   2
    //   / \  / \
    //  1  3 7  12

    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->left = new Node(7);
    root->right->right = new Node(12);

    root = correctBST(root);

    printTree(root);

    return 0;
}
Java
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Collections;

// Node structure
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

public class GFG {

    // Print tree in level order array format
    static void printTree(Node root) {
        if (root == null) {
            System.out.println("[]");
            return;
        }

        ArrayList<String> ans = new ArrayList<>();
        Queue<Node> q = new LinkedList<>();
        q.offer(root);

        while (!q.isEmpty()) {
            Node curr = q.poll();

            if (curr != null) {
                ans.add(String.valueOf(curr.data));
                q.offer(curr.left);
                q.offer(curr.right);
            } else {
                ans.add("N");
            }
        }

        while (!ans.isEmpty() && ans.get(ans.size() - 1).equals("N"))
            ans.remove(ans.size() - 1);

        System.out.print("[");
        for (int i = 0; i < ans.size(); i++) {
            if (i > 0)
                System.out.print(", ");
            System.out.print(ans.get(i));
        }
        System.out.println("]");
    }

    // Store inorder traversal
    static void findInorder(Node root, ArrayList<Integer> inorder) {
        if (root == null)
            return;

        findInorder(root.left, inorder);
        inorder.add(root.data);
        findInorder(root.right, inorder);
    }

    // Replace node values using sorted inorder array
    static void correctBSTUtil(Node root, ArrayList<Integer> inorder, int[] index) {
        if (root == null)
            return;

        correctBSTUtil(root.left, inorder, index);
        root.data = inorder.get(index[0]++);
        correctBSTUtil(root.right, inorder, index);
    }

    // Function to restore the BST
    static Node correctBST(Node root) {
        ArrayList<Integer> inorder = new ArrayList<>();

        findInorder(root, inorder);

        Collections.sort(inorder);

        int[] index = {0};
        correctBSTUtil(root, inorder, index);

        return root;
    }

    public static void main(String[] args) {

        // Constructing the tree with swapped nodes
        //       6
        //     /  \
        //    10   2
        //   / \  / \
        //  1  3 7  12

        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.left = new Node(7);
        root.right.right = new Node(12);

        root = correctBST(root);

        printTree(root);
    }
}
Python
from collections import deque

# Node structure
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


# Print tree in level order array format
def printTree(root):
    if root is None:
        print("[]")
        return

    ans = []
    q = deque()
    q.append(root)

    while q:
        curr = q.popleft()

        if curr is not None:
            ans.append(str(curr.data))
            q.append(curr.left)
            q.append(curr.right)
        else:
            ans.append("N")

    while ans and ans[-1] == "N":
        ans.pop()

    print("[" + ", ".join(ans) + "]")


# Store inorder traversal
def findInorder(root, inorder):
    if root is None:
        return

    findInorder(root.left, inorder)
    inorder.append(root.data)
    findInorder(root.right, inorder)


# Replace node values using sorted inorder array
def correctBSTUtil(root, inorder, index):
    if root is None:
        return

    correctBSTUtil(root.left, inorder, index)
    root.data = inorder[index[0]]
    index[0] += 1
    correctBSTUtil(root.right, inorder, index)


# Function to restore the BST
def correctBST(root):
    inorder = []

    findInorder(root, inorder)

    inorder.sort()

    index = [0]
    correctBSTUtil(root, inorder, index)

    return root


if __name__ == "__main__":

    # Constructing the tree with swapped nodes
    #       6
    #     /  \
    #    10   2
    #   / \  / \
    #  1  3 7  12

    root = Node(6)
    root.left = Node(10)
    root.right = Node(2)
    root.left.left = Node(1)
    root.left.right = Node(3)
    root.right.left = Node(7)
    root.right.right = Node(12)

    root = correctBST(root)

    printTree(root)
C#
using System;
using System.Collections.Generic;

// Node structure
class Node
{
    public int data;
    public Node left;
    public Node right;

    public Node(int x)
    {
        data = x;
        left = right = null;
    }
}

class GFG
{
    // Print tree in level order array format
    static void printTree(Node root)
    {
        if (root == null)
        {
            Console.WriteLine("[]");
            return;
        }

        List<string> ans = new List<string>();
        Queue<Node> q = new Queue<Node>();
        q.Enqueue(root);

        while (q.Count > 0)
        {
            Node curr = q.Dequeue();

            if (curr != null)
            {
                ans.Add(curr.data.ToString());
                q.Enqueue(curr.left);
                q.Enqueue(curr.right);
            }
            else
            {
                ans.Add("N");
            }
        }

        while (ans.Count > 0 && ans[ans.Count - 1] == "N")
            ans.RemoveAt(ans.Count - 1);

        Console.Write("[");
        for (int i = 0; i < ans.Count; i++)
        {
            if (i > 0)
                Console.Write(", ");
            Console.Write(ans[i]);
        }
        Console.WriteLine("]");
    }

    // Store inorder traversal
    static void findInorder(Node root, List<int> inorder)
    {
        if (root == null)
            return;

        findInorder(root.left, inorder);
        inorder.Add(root.data);
        findInorder(root.right, inorder);
    }

    // Replace node values using sorted inorder array
    static void correctBSTUtil(Node root, List<int> inorder, ref int index)
    {
        if (root == null)
            return;

        correctBSTUtil(root.left, inorder, ref index);
        root.data = inorder[index++];
        correctBSTUtil(root.right, inorder, ref index);
    }

    // Function to restore the BST
    static Node correctBST(Node root)
    {
        List<int> inorder = new List<int>();

        findInorder(root, inorder);

        inorder.Sort();

        int index = 0;
        correctBSTUtil(root, inorder, ref index);

        return root;
    }

    static void Main()
    {
        // Constructing the tree with swapped nodes
        //       6
        //     /  \
        //    10   2
        //   / \  / \
        //  1  3 7  12

        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.left = new Node(7);
        root.right.right = new Node(12);

        root = correctBST(root);

        printTree(root);
    }
}
JavaScript
// Node structure
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Print tree in level order array format
function printTree(root) {
    if (root === null) {
        console.log("[]");
        return;
    }

    let ans = [];
    let q = [];
    q.push(root);

    while (q.length > 0) {
        let curr = q.shift();

        if (curr !== null) {
            ans.push(curr.data.toString());
            q.push(curr.left);
            q.push(curr.right);
        } else {
            ans.push("N");
        }
    }

    while (ans.length > 0 && ans[ans.length - 1] === "N")
        ans.pop();

    console.log("[" + ans.join(", ") + "]");
}

// Store inorder traversal
function findInorder(root, inorder) {
    if (root === null)
        return;

    findInorder(root.left, inorder);
    inorder.push(root.data);
    findInorder(root.right, inorder);
}

// Replace node values using sorted inorder array
function correctBSTUtil(root, inorder, index) {
    if (root === null)
        return;

    correctBSTUtil(root.left, inorder, index);
    root.data = inorder[index.value++];
    correctBSTUtil(root.right, inorder, index);
}

// Function to restore the BST
function correctBST(root) {
    let inorder = [];

    findInorder(root, inorder);

    inorder.sort((a, b) => a - b);

    let index = { value: 0 };
    correctBSTUtil(root, inorder, index);

    return root;
}

// Driver code

// Constructing the tree with swapped nodes
//       6
//     /  \
//    10   2
//   / \  / \
//  1  3 7  12

let 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.left = new Node(7);
root.right.right = new Node(12);

root = correctBST(root);
printTree(root);

Output
[6, 2, 10, 1, 3, 7, 12]

[Expected Approach] Using One Traversal - O(n) Time and O(h) Space

The inorder traversal of a BST always gives the node values in sorted order. If exactly two nodes are swapped, this sorted order is broken at one or two places.

During the inorder traversal, keep track of the previously visited node. Whenever the current node has a smaller value than the previous node, a BST violation is found.

  • For the first violation, store the previous node as first and the current node as middle.
  • If another violation is found, store the current node as last.

After the traversal:

  • If both first and last are found, swap their values as the swapped nodes are non-adjacent.
  • Otherwise, swap the values of first and middle, which handles the case when the swapped nodes are adjacent in the inorder traversal.

Consider the following tree where 10 and 2 are swapped:

file

The inorder traversal is: 1 10 3 6 7 2 12

  • Visit 1 -> No violation.
  • Visit 10 -> No violation (10 > 1).
  • Visit 3 -> 3 < 10, so the first violation is found , first = 10, middle = 3
  • Visit 6 and 7 -> No violation.
  • Visit 2 -> 2 < 7, so the second violation is found, last = 2
  • Visit 12 -> No violation.

After the traversal: first = 10, middle = 3, last = 2

Since both first and last are found, swap their value

The inorder traversal becomes: 1 2 3 6 7 10 12

The corrected BST is:

file

Hence, the BST is restored successfully.

C++
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

// Node structure
class Node {
public:
    int data;
    Node* left;
    Node* right;

    Node(int x) {
        data = x;
        left = right = nullptr;
    }
};

// Print tree in level order array format
void printTree(Node* root) {
    if (root == nullptr) {
        cout << "[]\n";
        return;
    }

    vector<string> ans;
    queue<Node*> q;
    q.push(root);

    while (!q.empty()) {
        Node* curr = q.front();
        q.pop();

        if (curr != nullptr) {
            ans.push_back(to_string(curr->data));
            q.push(curr->left);
            q.push(curr->right);
        } else {
            ans.push_back("N");
        }
    }

    while (!ans.empty() && ans.back() == "N")
        ans.pop_back();

    cout << "[";
    for (int i = 0; i < ans.size(); i++) {
        if (i)
            cout << ", ";
        cout << ans[i];
    }
    cout << "]\n";
}

    // Performs inorder traversal to find the misplaced nodes.
    void correctBSTUtil(Node* root, Node*& first, Node*& middle, Node*& last,
                        Node*& prev) {
        if (root == nullptr)
            return;

        // Traverse left subtree
        correctBSTUtil(root->left, first, middle, last, prev);

        // Detect violation of BST property
        if (prev != nullptr && root->data < prev->data) {

            // First violation
            if (first == nullptr) {
                first = prev;
                middle = root;
            }
            // Second violation
            else {
                last = root;
            }
        }

        prev = root;

        // Traverse right subtree
        correctBSTUtil(root->right, first, middle, last, prev);
    }

    Node* correctBST(Node* root) {

        Node *first = nullptr, *middle = nullptr;
        Node *last = nullptr, *prev = nullptr;

        correctBSTUtil(root, first, middle, last, prev);

        // If the swapped nodes are non-adjacent
        if (first != nullptr && last != nullptr)
            swap(first->data, last->data);

        // If the swapped nodes are adjacent
        else if (first != nullptr && middle != nullptr)
            swap(first->data, middle->data);

        return root;
    }
int main() {

    // Constructing the tree with swapped nodes
    //       6
    //     /  \
    //    10   2
    //   / \  / \
    //  1  3 7  12

    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->left = new Node(7);
    root->right->right = new Node(12);

    root = correctBST(root);

    printTree(root);

    return 0;
}
Java
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;

// Node structure
class Node {
    int data;
    Node left;
    Node right;

    Node(int x) {
        data = x;
        left = right = null;
    }
}

class GFG {

    // Print tree in level order array format
    static void printTree(Node root) {
        if (root == null) {
            System.out.println("[]");
            return;
        }

        ArrayList<String> ans = new ArrayList<>();
        Queue<Node> q = new LinkedList<>();
        q.offer(root);

        while (!q.isEmpty()) {
            Node curr = q.poll();

            if (curr != null) {
                ans.add(String.valueOf(curr.data));
                q.offer(curr.left);
                q.offer(curr.right);
            } else {
                ans.add("N");
            }
        }

        while (!ans.isEmpty() && ans.get(ans.size() - 1).equals("N"))
            ans.remove(ans.size() - 1);

        System.out.print("[");
        for (int i = 0; i < ans.size(); i++) {
            if (i > 0)
                System.out.print(", ");
            System.out.print(ans.get(i));
        }
        System.out.println("]");
    }

    // Performs inorder traversal to find the misplaced nodes.
    static void correctBSTUtil(Node root, Node[] first, Node[] middle,
                               Node[] last, Node[] prev) {
        if (root == null)
            return;

        // Traverse left subtree
        correctBSTUtil(root.left, first, middle, last, prev);

        // Detect violation of BST property
        if (prev[0] != null && root.data < prev[0].data) {

            // First violation
            if (first[0] == null) {
                first[0] = prev[0];
                middle[0] = root;
            }

            // Second violation
            else {
                last[0] = root;
            }
        }

        prev[0] = root;

        // Traverse right subtree
        correctBSTUtil(root.right, first, middle, last, prev);
    }

    static Node correctBST(Node root) {

        Node[] first = new Node[1];
        Node[] middle = new Node[1];
        Node[] last = new Node[1];
        Node[] prev = new Node[1];

        correctBSTUtil(root, first, middle, last, prev);

        // If the swapped nodes are non-adjacent
        if (first[0] != null && last[0] != null) {
            int temp = first[0].data;
            first[0].data = last[0].data;
            last[0].data = temp;
        }

        // If the swapped nodes are adjacent
        else if (first[0] != null && middle[0] != null) {
            int temp = first[0].data;
            first[0].data = middle[0].data;
            middle[0].data = temp;
        }

        return root;
    }

    public static void main(String[] args) {

        // Constructing the tree with swapped nodes
        //       6
        //     /  \
        //    10   2
        //   / \  / \
        //  1  3 7  12

        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.left = new Node(7);
        root.right.right = new Node(12);

        root = correctBST(root);

        printTree(root);
    }
}
Python
from collections import deque

# Node structure
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


# Print tree in level order array format
def printTree(root):
    if root is None:
        print("[]")
        return

    ans = []
    q = deque()
    q.append(root)

    while q:
        curr = q.popleft()

        if curr is not None:
            ans.append(str(curr.data))
            q.append(curr.left)
            q.append(curr.right)
        else:
            ans.append("N")

    while ans and ans[-1] == "N":
        ans.pop()

    print("[" + ", ".join(ans) + "]")


# Performs inorder traversal to find the misplaced nodes.
def correctBSTUtil(root, first, middle, last, prev):
    if root is None:
        return

    # Traverse left subtree
    correctBSTUtil(root.left, first, middle, last, prev)

    # Detect violation of BST property
    if prev[0] is not None and root.data < prev[0].data:

        # First violation
        if first[0] is None:
            first[0] = prev[0]
            middle[0] = root

        # Second violation
        else:
            last[0] = root

    prev[0] = root

    # Traverse right subtree
    correctBSTUtil(root.right, first, middle, last, prev)


def correctBST(root):

    first = [None]
    middle = [None]
    last = [None]
    prev = [None]

    correctBSTUtil(root, first, middle, last, prev)

    # If the swapped nodes are non-adjacent
    if first[0] is not None and last[0] is not None:
        first[0].data, last[0].data = last[0].data, first[0].data

    # If the swapped nodes are adjacent
    elif first[0] is not None and middle[0] is not None:
        first[0].data, middle[0].data = middle[0].data, first[0].data

    return root


if __name__ == "__main__":

    # Constructing the tree with swapped nodes
    #       6
    #     /  \
    #    10   2
    #   / \  / \
    #  1  3 7  12

    root = Node(6)
    root.left = Node(10)
    root.right = Node(2)
    root.left.left = Node(1)
    root.left.right = Node(3)
    root.right.left = Node(7)
    root.right.right = Node(12)

    root = correctBST(root)

    printTree(root)
C#
using System;
using System.Collections.Generic;

// Node structure
class Node
{
    public int data;
    public Node left;
    public Node right;

    public Node(int x)
    {
        data = x;
        left = right = null;
    }
}

class GFG
{
    // Print tree in level order array format
    static void printTree(Node root)
    {
        if (root == null)
        {
            Console.WriteLine("[]");
            return;
        }

        List<string> ans = new List<string>();
        Queue<Node> q = new Queue<Node>();
        q.Enqueue(root);

        while (q.Count > 0)
        {
            Node curr = q.Dequeue();

            if (curr != null)
            {
                ans.Add(curr.data.ToString());
                q.Enqueue(curr.left);
                q.Enqueue(curr.right);
            }
            else
            {
                ans.Add("N");
            }
        }

        while (ans.Count > 0 && ans[ans.Count - 1] == "N")
            ans.RemoveAt(ans.Count - 1);

        Console.Write("[");
        for (int i = 0; i < ans.Count; i++)
        {
            if (i > 0)
                Console.Write(", ");
            Console.Write(ans[i]);
        }
        Console.WriteLine("]");
    }

    // Performs inorder traversal to find the misplaced nodes.
    static void correctBSTUtil(Node root, ref Node first, ref Node middle,
                               ref Node last, ref Node prev)
    {
        if (root == null)
            return;

        // Traverse left subtree
        correctBSTUtil(root.left, ref first, ref middle, ref last, ref prev);

        // Detect violation of BST property
        if (prev != null && root.data < prev.data)
        {
            // First violation
            if (first == null)
            {
                first = prev;
                middle = root;
            }

            // Second violation
            else
            {
                last = root;
            }
        }

        prev = root;

        // Traverse right subtree
        correctBSTUtil(root.right, ref first, ref middle, ref last, ref prev);
    }

    static Node correctBST(Node root)
    {
        Node first = null;
        Node middle = null;
        Node last = null;
        Node prev = null;

        correctBSTUtil(root, ref first, ref middle, ref last, ref prev);

        // If the swapped nodes are non-adjacent
        if (first != null && last != null)
        {
            int temp = first.data;
            first.data = last.data;
            last.data = temp;
        }

        // If the swapped nodes are adjacent
        else if (first != null && middle != null)
        {
            int temp = first.data;
            first.data = middle.data;
            middle.data = temp;
        }

        return root;
    }

    static void Main()
    {
        // Constructing the tree with swapped nodes
        //       6
        //     /  \
        //    10   2
        //   / \  / \
        //  1  3 7  12

        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.left = new Node(7);
        root.right.right = new Node(12);

        root = correctBST(root);

        printTree(root);
    }
}
JavaScript
// Node structure
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Print tree in level order array format
function printTree(root) {
    if (root === null) {
        console.log("[]");
        return;
    }

    let ans = [];
    let q = [];
    q.push(root);

    while (q.length > 0) {
        let curr = q.shift();

        if (curr !== null) {
            ans.push(curr.data.toString());
            q.push(curr.left);
            q.push(curr.right);
        } else {
            ans.push("N");
        }
    }

    while (ans.length > 0 && ans[ans.length - 1] === "N")
        ans.pop();

    console.log("[" + ans.join(", ") + "]");
}

// Performs inorder traversal to find the misplaced nodes.
function correctBSTUtil(root, first, middle, last, prev) {
    if (root === null)
        return;

    // Traverse left subtree
    correctBSTUtil(root.left, first, middle, last, prev);

    // Detect violation of BST property
    if (prev.node !== null && root.data < prev.node.data) {

        // First violation
        if (first.node === null) {
            first.node = prev.node;
            middle.node = root;
        }

        // Second violation
        else {
            last.node = root;
        }
    }

    prev.node = root;

    // Traverse right subtree
    correctBSTUtil(root.right, first, middle, last, prev);
}

function correctBST(root) {

    let first = { node: null };
    let middle = { node: null };
    let last = { node: null };
    let prev = { node: null };

    correctBSTUtil(root, first, middle, last, prev);

    // If the swapped nodes are non-adjacent
    if (first.node !== null && last.node !== null) {
        let temp = first.node.data;
        first.node.data = last.node.data;
        last.node.data = temp;
    }

    // If the swapped nodes are adjacent
    else if (first.node !== null && middle.node !== null) {
        let temp = first.node.data;
        first.node.data = middle.node.data;
        middle.node.data = temp;
    }

    return root;
}

// Driver code

// Constructing the tree with swapped nodes
//       6
//     /  \
//    10   2
//   / \  / \
//  1  3 7  12

let 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.left = new Node(7);
root.right.right = new Node(12);

root = correctBST(root);

printTree(root);

Output
[6, 2, 10, 1, 3, 7, 12]
Comment