Skip to content
Problem 99 · LeetCode 99(opens in a new tab)

Recover Binary Search Tree

MediumBinary search trees

You are handed a binary search tree (BST) through its root node. In a valid BST, every value in a node's left subtree is smaller than the node, and every value in its right subtree is larger. In this tree, the values of exactly two nodes were swapped by mistake.

Fix the tree in place by putting those two values back where they belong. Do not move, add or remove any node: the shape must stay exactly the same. The function returns nothing; the tree itself is checked.

Example 1

Input:  root = [5, 8, 3, 1, 4]
Output: [5, 3, 8, 1, 4]
Why:    8 sits left of 5 and 3 sits right of it; trade them
2 more examples and the constraints

Example 2

Input:  root = [6, 2, 10, null, null, 8, 9]
Output: [6, 2, 9, null, null, 8, 10]
Why:    10 and 9 are neighbours in sorted order and were swapped

Example 3

Input:  root = [1, 2]
Output: [2, 1]
Why:    a left child must be smaller than its parent

Constraints

  • 2 ≤ number of nodes ≤ 1,000
  • −2^31 ≤ Node.val ≤ 2^31 − 1, and all values are different
  • Exactly two values are out of place
  • Follow-up: use O(1) extra space

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Collect, sort, write backO(n log n)
  2. In-order walk with prevO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 1,000 nodesVerdict
Collect, sort, write backO(n log n)
Passes; uses O(n) memory
In-order walk with prevO(n)
Best; one walk, two pointers

n = number of nodes, h = height of the tree.

Edge cases to test

  • Swapped neighbours. In [6, 2, 10, null, null, 8, 9] the in-order list has a single drop, 10 > 9, so first and second both come from it.
  • The root is one of the two. [7, 2, 6, 1, 3, 5, 4] swaps the root with a leaf; values move, nodes do not.
  • Only two nodes. [1, 2] has one drop, and swapping fixes it.
  • Extreme values. Values can be −2^31 and 2^31 − 1, so do not use them as sentinels for “no previous node”.

Common mistakes

  • Taking node at the first drop and prev at the second, which swaps the wrong two values.
  • Assuming there are always two drops and missing the case where the swapped values are neighbours.
  • Starting prev at INT_MIN, which breaks when a node really holds −2^31.

How to talk through it

  1. 1Clarify. Exactly two values swapped? Values all different? Do I move values or nodes?
  2. 2Use the BST property. In-order gives a sorted list, so the swap shows up as drops in that list.
  3. 3Give the simple fix. Collect, sort, write back: O(n log n) time and O(n) space.
  4. 4Track only the drops. Keep prev; the first drop gives first, the last drop gives second; mention Morris for O(1) space.
  5. 5Test out loud. Run a far-apart swap, a neighbour swap and a swap that includes the root.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?