</> 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.

Insert into a Binary Search Tree

Core idea: A new key belongs in exactly one place โ€” the empty slot where a search for that key would fall off the tree. Walk down to that slot and hang the new leaf there.


Problem, rephrased

You maintain a leaderboard as a binary search tree keyed by score. Every node's left subtree holds smaller scores, every node's right subtree holds larger scores. A new player finishes their match with a score that isn't on the board yet. You need to slot that score into the tree so the BST ordering still holds, then hand back the root so the rest of the system can keep using the board.

The rules of the game:

  • The value is guaranteed not to already exist in the tree.
  • You return the root of the (modified) tree.
  • Any valid BST is an acceptable answer โ€” you do not have to rebalance or produce a "canonical" shape. As long as the in-order traversal stays sorted, you're correct.
Input (tree, in-order) Insert A valid output (in-order)
[2, 3, 4, 7] (root 4) 5 [2, 3, 4, 5, 7]
[] (empty) 9 [9]
[10, 20, 30] skewed right 25 [10, 20, 25, 30]

The output column shows the in-order traversal, which is what really matters: the shape may differ between solutions, but the sorted order never lies.


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.