Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given the root of a binary tree, each node in the tree has a distinct value.
After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).
Return the roots of the trees in the remaining forest. You may return the result in any order.
Example 1:
Input: root = [1,2,3,4,5,6,7], to_delete = [3,5] Output: [[1,2,null,4],[6],[7]]
Example 2:
Input: root = [1,2,4,null,3], to_delete = [3] Output: [[1,2,4]]
Constraints:
1000.1 and 1000.to_delete.length <= 1000to_delete contains distinct values between 1 and 1000.Problem summary: Given the root of a binary tree, each node in the tree has a distinct value. After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees). Return the roots of the trees in the remaining forest. You may return the result in any order.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Tree
[1,2,3,4,5,6,7] [3,5]
[1,2,4,null,3] [3]
count-nodes-with-the-highest-score)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1110: Delete Nodes And Return Forest
/**
* 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 {
private boolean[] s = new boolean[1001];
private List<TreeNode> ans = new ArrayList<>();
public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
for (int x : to_delete) {
s[x] = true;
}
if (dfs(root) != null) {
ans.add(root);
}
return ans;
}
private TreeNode dfs(TreeNode root) {
if (root == null) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left != null) {
ans.add(root.left);
}
if (root.right != null) {
ans.add(root.right);
}
return null;
}
}
// Accepted solution for LeetCode #1110: Delete Nodes And Return Forest
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func delNodes(root *TreeNode, to_delete []int) (ans []*TreeNode) {
s := make([]bool, 1001)
for _, x := range to_delete {
s[x] = true
}
var dfs func(*TreeNode) *TreeNode
dfs = func(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left = dfs(root.Left)
root.Right = dfs(root.Right)
if !s[root.Val] {
return root
}
if root.Left != nil {
ans = append(ans, root.Left)
}
if root.Right != nil {
ans = append(ans, root.Right)
}
return nil
}
if dfs(root) != nil {
ans = append(ans, root)
}
return
}
# Accepted solution for LeetCode #1110: Delete Nodes And Return Forest
# 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 delNodes(
self, root: Optional[TreeNode], to_delete: List[int]
) -> List[TreeNode]:
def dfs(root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return None
root.left, root.right = dfs(root.left), dfs(root.right)
if root.val not in s:
return root
if root.left:
ans.append(root.left)
if root.right:
ans.append(root.right)
return None
s = set(to_delete)
ans = []
if dfs(root):
ans.append(root)
return ans
// Accepted solution for LeetCode #1110: Delete Nodes And Return Forest
struct Solution;
use rustgym_util::*;
use std::collections::HashSet;
use std::iter::FromIterator;
trait Postorder {
fn postorder(self, nodes: &HashSet<i32>) -> (TreeLink, Vec<TreeLink>);
}
impl Postorder for TreeLink {
fn postorder(self, nodes: &HashSet<i32>) -> (TreeLink, Vec<TreeLink>) {
if let Some(node) = self {
let val = node.borrow_mut().val;
let left = node.borrow_mut().left.take();
let right = node.borrow_mut().right.take();
let (left_root, left_forest) = left.postorder(nodes);
let (right_root, right_forest) = right.postorder(nodes);
let mut forest = vec![];
for t in left_forest {
forest.push(t);
}
for t in right_forest {
forest.push(t);
}
if nodes.contains(&val) {
if left_root.is_some() {
forest.push(left_root);
}
if right_root.is_some() {
forest.push(right_root);
}
(None, forest)
} else {
node.borrow_mut().left = left_root;
node.borrow_mut().right = right_root;
(Some(node), forest)
}
} else {
(None, vec![])
}
}
}
impl Solution {
fn del_nodes(root: TreeLink, to_delete: Vec<i32>) -> Vec<TreeLink> {
let nodes = HashSet::from_iter(to_delete);
let mut res = vec![];
let (root, forest) = root.postorder(&nodes);
if root.is_some() {
res.push(root);
}
for t in forest {
res.push(t);
}
res
}
}
#[test]
fn test() {
let root = tree!(
1,
tree!(2, tree!(4), tree!(5)),
tree!(3, tree!(6), tree!(7))
);
let to_delete = vec![3, 5];
let res = vec![tree!(1, tree!(2, tree!(4), None), None), tree!(6), tree!(7)];
assert_eq!(Solution::del_nodes(root, to_delete), res);
}
// Accepted solution for LeetCode #1110: Delete Nodes And Return Forest
/**
* 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 delNodes(root: TreeNode | null, to_delete: number[]): Array<TreeNode | null> {
const s: boolean[] = Array(1001).fill(false);
for (const x of to_delete) {
s[x] = true;
}
const ans: Array<TreeNode | null> = [];
const dfs = (root: TreeNode | null): TreeNode | null => {
if (!root) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left) {
ans.push(root.left);
}
if (root.right) {
ans.push(root.right);
}
return null;
};
if (dfs(root)) {
ans.push(root);
}
return ans;
}
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: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
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.