Skip to content

Commit fb2a18c

Browse files
add a solution for 1110
1 parent 65d6adf commit fb2a18c

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/main/java/com/fishercoder/solutions/secondthousand/_1110.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.fishercoder.common.classes.TreeNode;
44

55
import java.util.ArrayList;
6+
import java.util.HashSet;
67
import java.util.LinkedList;
78
import java.util.List;
89
import java.util.Queue;
10+
import java.util.Set;
911

1012
public class _1110 {
1113
public static class Solution1 {
@@ -72,4 +74,54 @@ private boolean delete(TreeNode curr, int toDelete, Queue<TreeNode> queue) {
7274
}
7375
}
7476
}
77+
78+
public static class Solution2 {
79+
//use BFS
80+
public List<TreeNode> delNodes(TreeNode root, int[] toDelete) {
81+
Set<Integer> deleteSet = new HashSet<>();
82+
for (int d : toDelete) {
83+
deleteSet.add(d);
84+
}
85+
Queue<TreeNode> q = new LinkedList<>();
86+
q.offer(root);
87+
List<TreeNode> forest = new ArrayList<>();
88+
while (!q.isEmpty()) {
89+
TreeNode curr = q.poll();
90+
91+
//process left child if any
92+
if (curr.left != null) {
93+
//add it into the q first because we need to process it any ways as it might have children that might not need to be deleted
94+
q.offer(curr.left);
95+
if (deleteSet.contains(curr.left.val)) {
96+
curr.left = null;
97+
}
98+
}
99+
100+
//process right child if any
101+
if (curr.right != null) {
102+
q.offer(curr.right);
103+
if (deleteSet.contains(curr.right.val)) {
104+
curr.right = null;
105+
}
106+
}
107+
108+
//process this curr node: if it needs to be deleted, then add its non-null children into forest as we checked its children
109+
// and we know they do not need to be deleted at this point
110+
if (deleteSet.contains(curr.val)) {
111+
if (curr.left != null) {
112+
forest.add(curr.left);
113+
}
114+
if (curr.right != null) {
115+
forest.add(curr.right);
116+
}
117+
}
118+
//we don't add curr into forest here, otherwise there might be duplicate as we might have added them as their parent's child already
119+
}
120+
//at this point, only root might be missing, so we check root
121+
if (!deleteSet.contains(root.val)) {
122+
forest.add(root);
123+
}
124+
return forest;
125+
}
126+
}
75127
}

src/test/java/com/fishercoder/secondthousand/_1110Test.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions.secondthousand._1110;
6-
import org.junit.BeforeClass;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
88

99
import java.util.Arrays;
1010
import java.util.List;
1111

12-
import static junit.framework.TestCase.assertEquals;
1312

1413
public class _1110Test {
1514
private static _1110.Solution1 solution1;
15+
private static _1110.Solution2 solution2;
1616
private static TreeNode root;
1717

18-
@BeforeClass
19-
public static void setup() {
18+
@BeforeEach
19+
public void setup() {
2020
solution1 = new _1110.Solution1();
21+
solution2 = new _1110.Solution2();
2122
}
2223

2324
@Test
@@ -28,6 +29,12 @@ public void test1() {
2829
for (TreeNode node : actual) {
2930
TreeUtils.printBinaryTree(node);
3031
}
32+
actual.clear();
33+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
34+
actual = solution2.delNodes(root, new int[]{3, 5});
35+
for (TreeNode node : actual) {
36+
TreeUtils.printBinaryTree(node);
37+
}
3138
}
3239

3340
@Test
@@ -38,5 +45,11 @@ public void test2() {
3845
for (TreeNode node : actual) {
3946
TreeUtils.printBinaryTree(node);
4047
}
48+
actual.clear();
49+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 4, 3));
50+
actual = solution2.delNodes(root, new int[]{2, 3});
51+
for (TreeNode node : actual) {
52+
TreeUtils.printBinaryTree(node);
53+
}
4154
}
4255
}

0 commit comments

Comments
 (0)