Forgetting null/base-case handling
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.
Move from brute-force thinking to an efficient approach using tree strategy.
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 summary: 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).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Tree
[1,2,3,2,null,2,4] 2
[1,3,3,3,2] 3
[1,2,null,2,null,2] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1325: Delete Leaves With a Given Value
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode removeLeafNodes(TreeNode root, int target) {
if (root == null) {
return null;
}
root.left = removeLeafNodes(root.left, target);
root.right = removeLeafNodes(root.right, target);
if (root.left == null && root.right == null && root.val == target) {
return null;
}
return root;
}
}
// Accepted solution for LeetCode #1325: Delete Leaves With a Given Value
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func removeLeafNodes(root *TreeNode, target int) *TreeNode {
if root == nil {
return nil
}
root.Left = removeLeafNodes(root.Left, target)
root.Right = removeLeafNodes(root.Right, target)
if root.Left == nil && root.Right == nil && root.Val == target {
return nil
}
return root
}
# Accepted solution for LeetCode #1325: Delete Leaves With a Given Value
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def removeLeafNodes(
self, root: Optional[TreeNode], target: int
) -> Optional[TreeNode]:
if root is None:
return None
root.left = self.removeLeafNodes(root.left, target)
root.right = self.removeLeafNodes(root.right, target)
if root.left is None and root.right is None and root.val == target:
return None
return root
// Accepted solution for LeetCode #1325: Delete Leaves With a Given Value
struct Solution;
use rustgym_util::*;
trait Postorder {
fn postorder(self, target: i32) -> Self;
}
impl Postorder for TreeLink {
fn postorder(self, target: i32) -> Self {
if let Some(node) = self {
let val = node.borrow().val;
let left = node.borrow_mut().left.take();
let right = node.borrow_mut().right.take();
let left = left.postorder(target);
let right = right.postorder(target);
if left.is_none() && right.is_none() && val == target {
None
} else {
tree!(val, left, right)
}
} else {
None
}
}
}
impl Solution {
fn remove_leaf_nodes(root: TreeLink, target: i32) -> TreeLink {
root.postorder(target)
}
}
#[test]
fn test() {
let root = tree!(1, tree!(2, tree!(2), None), tree!(3, tree!(2), tree!(4)));
let target = 2;
let res = tree!(1, None, tree!(3, None, tree!(4)));
assert_eq!(Solution::remove_leaf_nodes(root, target), res);
let root = tree!(1, tree!(3, tree!(3), tree!(2)), tree!(3));
let target = 3;
let res = tree!(1, tree!(3, None, tree!(2)), None);
assert_eq!(Solution::remove_leaf_nodes(root, target), res);
let root = tree!(1, tree!(2, tree!(2, tree!(2), None), None), None);
let target = 2;
let res = tree!(1);
assert_eq!(Solution::remove_leaf_nodes(root, target), res);
}
// Accepted solution for LeetCode #1325: Delete Leaves With a Given Value
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function removeLeafNodes(root: TreeNode | null, target: number): TreeNode | null {
if (!root) {
return null;
}
root.left = removeLeafNodes(root.left, target);
root.right = removeLeafNodes(root.right, target);
if (!root.left && !root.right && root.val == target) {
return null;
}
return root;
}
Use this to step through a reusable interview workflow for this problem.
BFS with a queue visits every node exactly once — O(n) time. The queue may hold an entire level of the tree, which for a complete binary tree is up to n/2 nodes = O(n) space. This is optimal in time but costly in space for wide trees.
Every node is visited exactly once, giving O(n) time. Space depends on tree shape: O(h) for recursive DFS (stack depth = height h), or O(w) for BFS (queue width = widest level). For balanced trees h = log n; for skewed trees h = n.
Review these before coding to avoid predictable interview regressions.
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.