Delete Node in a BST
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt at Delete Node in a BST is forgetting to reattach the child pointer after the recursive call. Bloomberg's OA, reported in September 2020, hands you a valid BST and a key, and the tree has to stay a BST afterward. It's a tree problem, not a graph one, and the three deletion cases are the whole game. Leaf, one child, two children. You've likely seen it. The bug is in the details, not the idea. If you blank on the successor step during the live assessment, StealthCoder is the invisible safety net that gets you unstuck.
The problem
You are given the root of a binary search tree and an integer key. Delete the node whose value equals key, if it exists, and return the updated root. The returned tree must remain a binary search tree. When the deleted node has two children, replace its value with the smallest value in its right subtree, then delete that successor node. This deterministic canonical strategy fixes one serialized output for the runner. Function deleteNode(root: TreeNode, key: int) → TreeNode Examples Example 1 root = [5,3,6,2,4,null,7] key = 3 return = [5,4,6,2,null,null,7] Node 3 has two children. Its inorder successor is 4, which takes its place. Example 2 root = [5,3,6,2,4,null,7] key = 0 return = [5,3,6,2,4,null,7] The key is absent, so the tree is unchanged. Example 3 root = [] key = 0 return = [] An empty tree remains empty. Constraints The tree contains between 0 and 10000 nodes. -100000 <= Node.val <= 100000. Every node value is unique and the input is a valid binary search tree. -100000 <= key <= 100000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Recurse using BST ordering. If key is less than root.val, set root.left to deleteNode(root.left, key). If greater, do the same on the right. When you hit the match, handle three cases. No left child: return root.right. No right child: return root.left. Two children: walk to the leftmost node of the right subtree, copy its value into the current node, then delete that value from the right subtree. The common pitfall is dropping the return assignment, so the parent never updates and the tree looks unchanged. Another is deleting the wrong node after copying the successor value, or searching the whole tree instead of the right subtree. Handle a null root first, since an empty tree stays empty. Time is O(h), space is O(h) for the recursion stack. The problem fixes the successor strategy, so don't use the predecessor, or your output won't match the expected serialization. If the recursion tangles on the live OA, StealthCoder can give you the clean version as a hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Delete Node in a BST cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as delete node in a bst. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Delete Node in a BST FAQ
How hard is Delete Node in a BST really?+
Medium, but it's mostly bookkeeping. The idea is simple: search by BST order, then handle leaf, one child, and two children. Most failures come from pointer reassignment, not from the concept. If you can write a BST search recursively, you can finish this in one pass.
What's the trick for the two-child case?+
Find the smallest value in the right subtree by going left until you can't. Copy that value into the node you're deleting. Then recursively delete that value from the right subtree. The problem states this successor rule explicitly, so use it, not the predecessor.
Why does my output not match the expected tree?+
Usually you either didn't assign the recursive result back to root.left or root.right, or you used the inorder predecessor instead of the successor. The runner compares one serialized output, so the replacement choice matters. Check both before anything else.
What edge cases should I test?+
Empty tree, key not present, deleting the root, deleting a leaf, and a node with only one child. Example 3 covers the empty tree and Example 2 covers the missing key. Also try a root with only a right child, since that path returns root.right directly.
How do I prepare in 48 hours for a Bloomberg tree question like this?+
Write this one from memory twice, recursively. Then do BST insert and search so the ordering logic feels automatic. Focus on returning the updated subtree root from every call. That habit covers most BST modification questions you'd see on an OA.