Delete Leaves With a Target Value
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google flagged this one in September 2026, and the detail that trips people is the cascade: delete a target-valued leaf and its parent can become a new target-valued leaf that also has to go. It's a binary tree problem with a postorder answer. If you've seen it before, it's ten lines. If you haven't, the cascade feels like it needs a loop or a second pass, and it doesn't. Tree size goes up to 100000 nodes, so depth matters too. Keep StealthCoder running as a safety net on the live OA in case your mind goes blank on the recursion order.
The problem
Given a binary tree and an integer target, repeatedly delete every leaf whose value equals target. After deletions, a parent may become a new target-valued leaf and must also be deleted. Return the final root. Modify and reuse the surviving input nodes. Function removeLeafNodes(root: TreeNode, target: int) → TreeNode Examples Example 1 root = [1,2,3,2,null,2,4] target = 2 return = [1,null,3,null,4] All target-valued leaves are removed, including the left child after its child disappears. Example 2 root = [1,3,3,3,2] target = 3 return = [1,3,null,null,2] The left 3 remains because it still has a non-target child. Example 3 root = [1,2,null,2,null,2] target = 2 return = [1] Deletion cascades upward through the chain of 2-valued nodes. Example 4 root = [] target = 1 return = [] An empty tree remains empty. Constraints The tree contains between 0 and 100000 nodes. -10^9 <= node.val, target <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is processing children before the parent. Recurse into left and right first, and assign the results back to node.left and node.right. Then check the current node: if both children are now null and node.val equals target, return null. Otherwise return the node. That single postorder pass handles the cascade, because by the time you judge a parent, its children have already been pruned. The common pitfall is checking the node before recursing, which misses the chains in Example 3 and forces a repeat loop. Another pitfall is forgetting to reassign the child pointers, so the deletion never sticks. With up to 100000 nodes, a skewed tree can make recursion deep, so mention an iterative option or note the stack depth. Time is O(n), space is O(h). If the postorder ordering slips away mid-assessment, StealthCoder is the hedge that hands you the working solution while you stay in the editor.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Delete Leaves With a Target Value 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Delete Leaves With a Target Value FAQ
What's the trick to Delete Leaves With a Target Value?+
Use postorder. Recurse into both children first, reassign them to node.left and node.right, then decide about the current node. If it's now a leaf with value equal to target, return null. That handles the cascading deletions in one pass with no repeated sweeps.
How hard is this problem really?+
It's an easy-to-medium tree problem. The code is short, but people lose time by checking the node before recursing. Once you commit to children-first, it falls out. Expect to write it in a few minutes if you've seen postorder pruning before.
Why does the parent get deleted in Example 3?+
Because the deepest 2 is removed first, which turns its parent into a leaf with value 2. Postorder sees that updated state on the way back up, so the whole chain collapses until only the root 1 remains.
Do I need to worry about recursion depth with 100000 nodes?+
Yes, a fully skewed tree can hit 100000 levels deep, which may overflow the stack in some languages. Recursion is the expected answer, but mention that an iterative postorder with an explicit stack avoids the risk if the interviewer asks.
How do I prepare for this in 48 hours?+
Write the postorder prune from memory three times: this one, plus a variant like pruning a subtree. Practice the pattern of assigning recursive results back to child pointers. Test the empty tree and the all-target chain cases before the OA.