</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Modify Structure

What is this?

Here you change a BST โ€” add a node, remove one, or fix a broken one โ€” without ruining the left-smaller, right-larger rule. Reading a sorted tree is easy; the skill is editing it so it stays sorted. As long as every value keeps its proper place, the tree still works like a sorted list you can search instantly.

flowchart TD A["Find the target node"] --> B["Insert at empty leaf slot"] A --> C["Delete and fill the hole"] A --> D["Repair an out-of-order pair"]

๐Ÿ’ก Fun fact: Deleting a node with two children is the famously tricky case. The fix is to swap it with its inorder successor โ€” the smallest value in its right subtree โ€” which is guaranteed to slot in without disturbing the order.

๐Ÿ”“ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: Reading a BST is easy; changing one without breaking the ordering invariant is where the real care lives. Every edit here is the same discipline โ€” first find the node by descending on comparisons, then restructure locally while guaranteeing that smaller-left and larger-right still holds for the whole tree. The hard part is never the search; it is the surgery that keeps the invariant intact.


The pattern

All three problems start with the familiar O(h) descent: compare against the node, go left or right. What differs is what you do when you arrive. Insertion finds an empty slot and hangs a node there. Deletion has to fill the hole left behind without scrambling order. Repair walks the whole tree to spot where order was already violated.

The unifying idea is the inorder successor โ€” the next-largest key, found by stepping right once and then left as far as possible. It is the value that can safely replace a deleted node, and the inorder traversal it comes from is also what exposes a corrupted tree. Master "find the successor" and "preserve the sorted inorder sequence" and the chapter falls into place.


The problems

  • Insert into a Binary Search Tree โ€” the gentle case. Walk down to the leaf slot where the key belongs (left when smaller, right when larger) and attach a new node there. No existing node moves, so the invariant is preserved for free.
  • Delete Node in a BST โ€” the centerpiece, with three cases. A leaf is simply removed; a node with one child is replaced by that child; a node with two children is replaced by its inorder successor (smallest key in the right subtree), then that successor is deleted from where it originally sat. The successor swap is what keeps the ordering valid.
  • Recover Binary Search Tree โ€” two nodes were swapped by mistake; restore the tree. An inorder traversal of a valid BST is strictly increasing, so the swap shows up as one or two out-of-order dips. Identify the two offending nodes, swap their values back, and you are done โ€” no restructuring needed.

Key takeaways

  • Find first, then restructure โ€” the search is the easy part; protecting the invariant during the edit is the skill.
  • Insertion attaches at a leaf slot, so no existing node ever has to move.
  • Deletion has three cases; the two-child case hinges on the inorder successor swap.
  • The inorder successor is the right-once-then-left-all-the-way node โ€” learn it once, reuse it everywhere.
  • Recovery rides the inorder sequence: a valid BST is strictly increasing, so swapped nodes appear as out-of-order dips.
  • Most edits stay O(h); only the full repair scan touches every node in O(n).

Order: Insert into a Binary Search Tree โ†’ Delete Node in a BST โ†’ Recover Binary Search Tree.

Core idea: Inorder of a BST is strictly increasing, so two swapped values leave behind one or two "dips" โ€” the culprits are the prev of the first dip and the curr of the last dip.

Problem, rephrased

You maintain a balanced index of account IDs, kept in a binary search tree so that an inorder walk yields them in perfect ascending order. A buggy migration script ran overnight and accidentally transposed the values of exactly two nodes. The tree's shape is untouched โ€” every pointer is where it always was โ€” but two nodes now hold each other's value, so the BST invariant is violated.

Your job: put the BST back in order by swapping those two values back, in place, without rebuilding or rotating anything.

Input (BST with two swapped values) Output (repaired BST)
[1, 3, null, null, 2] [3, 1, null, null, 2]
[3, 1, 4, null, null, 2] [2, 1, 4, null, null, 3]
Single node [5] [5] (nothing to do)

You return nothing; you mutate the tree in place.

Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.