Watch 10 video solutions for Delete Leaves With a Given Value, a medium level problem involving Tree, Depth-First Search, Binary Tree. This walkthrough by NeetCodeIO has 10,136 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given a binary tree root and an integer target, delete all the leaf nodes with value target.
Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot).
Example 1:

Input: root = [1,2,3,2,null,2,4], target = 2 Output: [1,null,3,null,4] Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left). After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).
Example 2:

Input: root = [1,3,3,3,2], target = 3 Output: [1,3,null,null,2]
Example 3:

Input: root = [1,2,null,2,null,2], target = 2 Output: [1] Explanation: Leaf nodes in green with value (target = 2) are removed at each step.
Constraints:
[1, 3000].1 <= Node.val, target <= 1000Problem Overview: Given the root of a binary tree and a target value, remove every leaf node whose value equals the target. After deleting a leaf, its parent might become a new leaf that also matches the target, so the process continues until no such leaves remain.
Approach 1: Recursive Depth-First Search (Post-Order DFS) (Time: O(n), Space: O(h))
The cleanest solution uses depth-first search with a post-order traversal. Traverse to the left and right children first, then evaluate the current node. After recursion returns, check whether both children are null and the node value equals the target. If so, return null to the parent, effectively deleting the node.
The key insight: a node can only become a removable leaf after its children are processed. Post-order traversal guarantees that children are handled before their parent. Each node is visited once, so the total work is linear. The recursion stack grows proportional to the tree height h, which is O(log n) for balanced trees and O(n) in the worst case.
This approach works naturally with recursive tree processing and is commonly used in tree and binary tree problems where parent decisions depend on child results.
Approach 2: Iterative Post-Order Traversal (Time: O(n), Space: O(n))
An iterative solution simulates post-order traversal using an explicit stack. Push nodes while traversing down the tree and track whether the node has already visited its children. When both children have been processed, evaluate whether the node is now a leaf with the target value.
If the node should be deleted, update its parent pointer in the stack structure to remove the reference. This approach mirrors the recursive logic but manages traversal state manually. The main advantage is avoiding recursion limits in languages or environments where stack depth could be large.
Because every node is pushed and processed once, the traversal still runs in O(n) time. The explicit stack can hold up to O(n) nodes in skewed trees.
Recommended for interviews: Recursive DFS with post-order traversal. It expresses the core idea directly: process children first, then decide whether the current node becomes a removable leaf. Showing the recursive version demonstrates strong understanding of tree recursion. The iterative version is useful when interviewers specifically ask for a non-recursive traversal or stack-based implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Depth-First Search (Post-Order) | O(n) | O(h) | Best general solution. Simple logic when using recursion on binary trees. |
| Iterative Post-Order Traversal | O(n) | O(n) | Useful when recursion depth could be large or when an interviewer asks for an iterative traversal. |