Delete Leaves With a Given Value (LeetCode 1325): Solution and Explanation
Delete every leaf equal to target, including parents that turn into such leaves after their children are removed. A single post-order walk catches the whole cascade.
Intuition
The tricky part is the cascade: deleting a leaf can turn its parent into a new leaf, and if that parent also equals the target it has to go too. Scanning the tree top-down would miss those parents unless you repeat the scan.
The fix is to decide each node only after both of its children have been decided. By the time a node is judged, any child that was going to disappear is already gone, so the node knows whether it is a leaf right now.
Returning null for a deleted node lets the parent drop it with a plain assignment, root.left = removeLeafNodes(root.left, target). The cascade then climbs the tree on its own as the recursion unwinds.
Approach: Post-order DFS
- If
rootis null, return null. - Recurse into the left child and store the result in
root.left. - Recurse into the right child and store the result in
root.right. - If
root.val == targetand both children are now null, return null. - Otherwise return
root.
Step-by-step example
root = [1,2,3,2,null,2,4], target = 2
- Start at the root 1 and go left to the 2, then left again to the leaf 2.
- That leaf equals the target, so it returns null and its parent's left pointer is cleared.
- The parent 2 has no right child either, so it is now a leaf equal to 2. It returns null as well: this is the cascade.
- On the right side, the leaf 2 under 3 is deleted and the leaf 4 is kept. The 3 still has the child 4, and it is not the target anyway, so it stays.
- The root 1 is not the target, so it stays. The result is [1,null,3,null,4].
Code: Python, Java, C++
1class Solution:
2 def removeLeafNodes(self, root: Optional[TreeNode], target: int) -> Optional[TreeNode]:
3 if not root:
4 return None
5 root.left = self.removeLeafNodes(root.left, target)
6 root.right = self.removeLeafNodes(root.right, target)
7 if root.val == target and not root.left and not root.right:
8 return None
9 return root1class Solution {
2 public TreeNode removeLeafNodes(TreeNode root, int target) {
3 if (root == null) return null;
4 root.left = removeLeafNodes(root.left, target);
5 root.right = removeLeafNodes(root.right, target);
6 if (root.val == target && root.left == null && root.right == null) return null;
7 return root;
8 }
9}1class Solution {
2public:
3 TreeNode* removeLeafNodes(TreeNode* root, int target) {
4 if (!root) return nullptr;
5 root->left = removeLeafNodes(root->left, target);
6 root->right = removeLeafNodes(root->right, target);
7 if (root->val == target && !root->left && !root->right) return nullptr;
8 return root;
9 }
10};Time and space complexity
O(n)
Each node is visited exactly once and does constant work after its children return.
O(h)
The recursion stack is as deep as the tree height h, which can be n for a chain.
Other approaches
Repeat leaf removal until stable (time O(n * h), space O(h))
Remove every target leaf in one pass, then scan again, until a pass deletes nothing. It works but a chain of k target nodes needs k passes.
Iterative post-order with parent pointers (time O(n), space O(h))
Walk the tree in post-order with an explicit stack and detach a node from its parent when it is a target leaf. Same idea without recursion, useful for very deep trees.
Common mistakes
- Checking whether a node is a leaf before recursing into its children. Its children may still be deleted, which would make it a leaf afterward.
- Forgetting to assign the recursive results back to
root.leftandroot.right, so deleted nodes stay attached. - Deleting a target node that still has a surviving child. Only leaves are deleted.
- Not handling the case where the root itself is deleted, which makes the answer an empty tree.
Frequently asked questions
Why is one pass enough?
In post-order a node is judged after its whole subtree is final. If it becomes a leaf because of deletions below, that has already happened by the time it is checked.
Can the answer be empty?
Yes. If every node equals the target, deletions cascade all the way up and the root is deleted too, so the function returns null.
Is a target node with a child ever deleted?
Only if that child is deleted first. A target node whose child survives is not a leaf, so it stays.
How is this different from Binary Tree Pruning?
It is the same post-order shape. Pruning removes zero-valued subtrees with no 1 inside, while here the rule is a leaf whose value equals the given target.