FastPrepRecover Binary Search Tree
Problems/Recover Binary Search Tree

Recover Binary Search Tree

Globalization Partners logoGlobalization Partners● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report links the well-known Recover Binary Search Tree task. Our function returns the recovered root so it can be checked. Our version is about 99% the same as the reported question.

You are given the root of a binary search tree in which the values of exactly two nodes were swapped by mistake. Restore the binary search tree without changing its structure.

Return the same root after you fix the tree. The tests read the fixed tree to check it.

Function

recoverTree(root: TreeNode) → TreeNode

Examples

Example 1

root = [1,3,null,null,2]return = [3,1,null,null,2]
root
132
→
return
312
Moved or new

The value 3 cannot be the left child of 1 in a valid binary search tree. Swapping 1 and 3 restores the ordering.

Example 2

root = [3,1,4,null,null,2]return = [2,1,4,null,null,3]
root
3142
→
return
2143
Moved or new

The inorder sequence is [1,3,2,4]. Swapping the misplaced values 3 and 2 makes it increasing.

Example 3

root = [2,3,1]return = [2,1,3]
root
231
→
return
213
Moved or new

The two child values are reversed. Swapping them preserves the three-node structure and restores the tree.

Constraints

  • The tree contains between 2 and 1000 nodes.
  • -2^31 <= Node.val <= 2^31 - 1
  • The input was a valid binary search tree before exactly two node values were exchanged.

More Globalization Partners Onsite Interview questions

  • Binary Tree Maximum Path SumONSITE INTERVIEW · Seen Jun 2026
  • Maximum Number of Fish in a GridONSITE INTERVIEW · Seen Jun 2026

Similar questions at other companies

  • Merge N-Ary Trees by KeyLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
  • Aggregate Machine TopologyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
  • Vertical Order Traversal of a Binary TreeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Vertical Order Traversal of a Binary TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Build a Trace ForestDatadog · PHONE SCREENDatadog interview questions
  • Analyze Underpaid Managers in a Salary TreeGoogle · PHONE SCREENGoogle interview questions
  • Vertical Tree Traversal with Value TiesMeta · PHONE SCREENMeta interview questions
See Globalization Partners hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode recoverTree(TreeNode root) {
        // write your code here
    }
}
Checking account…