Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6a5e6a3

Browse files
add 1110
1 parent95e28ce commit6a5e6a3

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ _If you like this project, please leave me a star._ ★
243243
|1119|[Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1119.java)|[:tv:](https://www.youtube.com/watch?v=6KCBrIWEauw)|Easy||
244244
|1118|[Number of Days in a Month](https://leetcode.com/problems/number-of-days-in-a-month/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1118.java)||Easy||
245245
|1114|[Print in Order](https://leetcode.com/problems/print-in-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1114.java)||Easy||
246+
|1110|[Delete Nodes And Return Forest](https://leetcode.com/problems/delete-nodes-and-return-forest/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1110.java)||Medium|Tree, DFS|
246247
|1108|[Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1108.java)|[:tv:](https://www.youtube.com/watch?v=FP0Na-pL0qk)|Easy||
247248
|1104|[Path In Zigzag Labelled Binary Tree](https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1104.java)||Medium|Math, Tree|
248249
|1103|[Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1103.java)||Easy|Math|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.LinkedList;
7+
importjava.util.List;
8+
importjava.util.Queue;
9+
10+
publicclass_1110 {
11+
publicstaticclassSolution1 {
12+
publicList<TreeNode>delNodes(TreeNoderoot,int[]to_delete) {
13+
Queue<TreeNode>queue =newLinkedList<>();
14+
queue.offer(root);
15+
for (intd :to_delete) {
16+
delete(d,queue);
17+
}
18+
List<TreeNode>result =newArrayList<>();
19+
while (!queue.isEmpty()) {
20+
result.add(queue.poll());
21+
}
22+
returnresult;
23+
}
24+
25+
privatevoiddelete(inttoDelete,Queue<TreeNode>queue) {
26+
intsize =queue.size();
27+
for (inti =0;i <size;i++) {
28+
TreeNodecurr =queue.poll();
29+
if (delete(curr,toDelete,queue)) {
30+
if (toDelete !=curr.val) {
31+
queue.offer(curr);
32+
}
33+
break;
34+
}else {
35+
queue.offer(curr);
36+
}
37+
}
38+
}
39+
40+
privatebooleandelete(TreeNodecurr,inttoDelete,Queue<TreeNode>queue) {
41+
if (curr ==null) {
42+
returnfalse;
43+
}else {
44+
if (curr.val ==toDelete) {
45+
if (curr.left !=null) {
46+
queue.offer(curr.left);
47+
}
48+
if (curr.right !=null) {
49+
queue.offer(curr.right);
50+
}
51+
returntrue;
52+
}elseif (curr.left !=null &&curr.left.val ==toDelete) {
53+
if (curr.left.left !=null) {
54+
queue.offer(curr.left.left);
55+
}
56+
if (curr.left.right !=null) {
57+
queue.offer(curr.left.right);
58+
}
59+
curr.left =null;
60+
returntrue;
61+
}elseif (curr.right !=null &&curr.right.val ==toDelete) {
62+
if (curr.right.left !=null) {
63+
queue.offer(curr.right.left);
64+
}
65+
if (curr.right.right !=null) {
66+
queue.offer(curr.right.right);
67+
}
68+
curr.right =null;
69+
returntrue;
70+
}
71+
returndelete(curr.left,toDelete,queue) ||delete(curr.right,toDelete,queue);
72+
}
73+
}
74+
}
75+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
5+
importcom.fishercoder.common.utils.TreeUtils;
6+
importcom.fishercoder.solutions._1110;
7+
importcom.fishercoder.solutions._206;
8+
importorg.junit.BeforeClass;
9+
importorg.junit.Test;
10+
11+
importjava.util.Arrays;
12+
importjava.util.List;
13+
14+
importstaticjunit.framework.TestCase.assertEquals;
15+
16+
publicclass_1110Test {
17+
privatestatic_1110.Solution1solution1;
18+
privatestaticTreeNoderoot;
19+
20+
@BeforeClass
21+
publicstaticvoidsetup() {
22+
solution1 =new_1110.Solution1();
23+
}
24+
25+
@Test
26+
publicvoidtest1() {
27+
root =TreeUtils.constructBinaryTree(Arrays.asList(1,2,3,4,5,6,7));
28+
TreeUtils.printBinaryTree(root);
29+
List<TreeNode>actual =solution1.delNodes(root,newint[]{3,5});
30+
for (TreeNodenode :actual) {
31+
TreeUtils.printBinaryTree(node);
32+
}
33+
}
34+
35+
@Test
36+
publicvoidtest2() {
37+
root =TreeUtils.constructBinaryTree(Arrays.asList(1,2,null,4,3));
38+
TreeUtils.printBinaryTree(root);
39+
List<TreeNode>actual =solution1.delNodes(root,newint[]{2,3});
40+
for (TreeNodenode :actual) {
41+
TreeUtils.printBinaryTree(node);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp