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

Delete Node in a BST

Core idea: Walk down to the node like any BST search, then patch the hole it leaves โ€” and if it has two children, plug it with its inorder successor so the sorted order never breaks.

Deleting from a BST trips up more candidates than inserting into one, because removing a node can tear a hole in the middle of the tree. You can't just unlink it โ€” you have to ask who takes its place? The answer is a small, three-case decision that, once you see it, you'll never forget.


Problem, rephrased

You maintain a leaderboard index keyed by player rating. It's a binary search tree: every node's left subtree holds smaller ratings, its right subtree holds larger ones. A player just quit, and you need to evict their rating from the index while keeping it a valid BST so future range queries ("show me everyone between 1500 and 1800") still work.

Given the root of the BST and a key (the rating to remove):

  • Find the node whose value equals key.
  • Remove it, restitching the tree so it stays a valid BST.
  • Return the (possibly new) root.

If the key isn't present, return the tree unchanged.

Input (BST, drawn) key Output (BST, drawn) Why
5 / 3 6 / 2 4 \ 7 3 5 / 4 6 / 2 \ 7 Node 3 has two children; successor 4 slides up
5 / 3 6 \ 7 6 5 / 3 7 Node 6 has one child; child 7 splices up
5 / 3 6 6 5 / 3 Node 6 is a leaf; just drop it

For the two-child case there can be more than one valid answer (successor or predecessor both work). We'll commit to the inorder successor throughout.


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.