You are given the root
of a binary tree, where each node in the tree has a distinct value.
After deleting all nodes with a value in to_delete
, you are left with a forest (a disjoint union of trees).
Can you return the roots of the trees in the remaining forest? You may return the result in any order.
Example 1:
Consider the following binary tree:
1
/ \
2 3
/ \
4 5
If to_delete = [3, 5]
, the expected output is [[1, 2, null, 4], [6], [7]]
.
Example 2:
Consider the following binary tree:
1
/ \
2 4
\
3
If to_delete = [3]
, the expected output is [[1, 2, 4]]
.
Constraints:
1000
.1
and 1000
.to_delete.length <= 1000
to_delete
contains distinct values between 1
and 1000
.Let's analyze the problem. We're given a binary tree and a list of node values to delete. After deleting these nodes, we're left with a forest, which is a collection of disjoint trees. The goal is to return the roots of these remaining trees.
A brute-force approach would involve the following steps:
This approach involves multiple traversals of the tree, resulting in potentially higher time complexity.
A more efficient approach involves a single traversal of the tree using recursion. During the traversal, we check if the current node's value is in the to_delete
list. If it is, we remove the node and add its children (if they exist) to the result list as new roots. If it isn't, we continue the traversal, ensuring that the parent-child relationship is maintained correctly.
Algorithm:
HashSet
from the to_delete
array for efficient lookups.HashSet
as input.null
, return null
.HashSet
.
null
to remove the current node from the tree.Edge Cases:
null
, return an empty list.to_delete
, return an empty list.to_delete
is empty, return a list containing the original root.Code (Java):
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
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 List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
List<TreeNode> result = new ArrayList<>();
Set<Integer> toDeleteSet = new HashSet<>();
for (int val : to_delete) {
toDeleteSet.add(val);
}
root = helper(root, toDeleteSet, result);
if (root != null) {
result.add(root);
}
return result;
}
private TreeNode helper(TreeNode node, Set<Integer> toDeleteSet, List<TreeNode> result) {
if (node == null) {
return null;
}
node.left = helper(node.left, toDeleteSet, result);
node.right = helper(node.right, toDeleteSet, result);
if (toDeleteSet.contains(node.val)) {
if (node.left != null) {
result.add(node.left);
}
if (node.right != null) {
result.add(node.right);
}
return null;
} else {
return node;
}
}
}
Time Complexity:
Space Complexity:
to_delete
(for the HashSet).