Tutorialspoint
Problem
Solution
Submissions

Recover Binary Search Tree

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to recover a Binary Search Tree (BST) where exactly two nodes have been swapped incorrectly. You need to restore the tree without changing its structure, only by correcting the swapped nodes. A BST is a binary tree where for each node, all elements in its left subtree are less than the node's value, and all elements in its right subtree are greater than the node's value.

Example 1
  • Input: root = [1,3,null,null,2]
  • Output: [3,1,null,null,2]
  • Explanation:
    • Step 1: The BST [1,3,null,null,2] is represented as:

      Input of Example one of Recover Binary Search Tree


    • Step 2: This is not a valid BST because 3 > 1, so the left child should be less than its parent.
    • Step 3: We identify that 1 and 3 have been swapped.
    • Step 4: After swapping them back, the corrected BST is:

      Output of Example one of Recover Binary Search Tree
    • Step 5: This is now a valid BST.
Example 2
  • Input: root = [3,1,4,null,null,2]
  • Output: [3,1,4,null,null,2]
  • Explanation:
    • Step 1: The BST [3,1,4,null,null,2] is represented as:

      Input of Example two of Recover Binary Search Tree

  • Step 2: This is not a valid BST because 2 < 3, but it's in the right subtree.
  • Step 3: We identify that 2 and 4 have been swapped.
  • Step 4: After swapping them back, the corrected BST is:

    Output of Example two of Recover Binary Search Tree

  • Step 5: This is now a valid BST.
  • Constraints
    • The number of nodes in the tree is in the range [2, 1000]
    • -2^31 ≤ Node.val ≤ 2^31 - 1
    • All values in the tree are unique
    • Exactly two nodes have been swapped
    • Time Complexity: O(n)
    • Space Complexity: O(h) where h is the height of the tree (recursion stack)
    TreeWiproGoldman Sachs
    Editorial

    Login to view the detailed solution and explanation for this problem.

    My Submissions
    All Solutions
    Lang Status Date Code
    You do not have any submissions for this problem.
    User Lang Status Date Code
    No submissions found.

    Please Login to continue
    Solve Problems

     
     
     
    Output Window

    Don't have an account? Register

    Solution Hints

    • Perform an in-order traversal of the tree, which should visit nodes in ascending order in a valid BST.
    • During traversal, identify the nodes that are out of order.
    • There will be either one or two violations where a node is greater than its successor.
    • If there's one violation, the two swapped nodes are adjacent in the in-order traversal.
    • If there are two violations, the first node of the first violation and the second node of the second violation are the swapped ones.

    Steps to solve by this approach:

     Step 1: Initialize three pointers: 'first' and 'second' to track the swapped nodes, and 'prev' to keep track of the previously visited node during in-order traversal.
    
     Step 2: Perform an in-order traversal of the BST, which should visit nodes in ascending order in a valid BST.
     Step 3: During traversal, compare each node with the previously visited node. In a valid BST, the current node's value should be greater than the previous node's value.
     Step 4: When a violation is found (prev.val > root.val), mark the nodes: - If it's the first violation, store the previous node as 'first' - Store the current node as 'second' (this will be updated if a second violation is found)
     Step 5: After traversal, we've identified the two nodes that were swapped.
     Step 6: Swap the values of the 'first' and 'second' nodes to recover the BST.
     Step 7: The BST is now restored without changing its structure.

    Submitted Code :