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

Commit7bf365e

Browse files
add 1273
1 parenta421220 commit7bf365e

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ _If you like this project, please leave me a star._ ★
4444
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java)||Easy||
4545
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java)||Medium||
4646
|1275|[Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1275.java)||Easy|Array|
47+
|1273|[Delete Tree Nodes](https://leetcode.com/problems/delete-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1273.java)||Medium|Dynamic Programming, DFS|
4748
|1271|[Hexspeak](https://leetcode.com/problems/hexspeak/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1271.java)||Easy||
4849
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1267.java)||Medium||
4950
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java)||Easy||
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
5+
/**
6+
* 1273. Delete Tree Nodes
7+
*
8+
* A tree rooted at node 0 is given as follows:
9+
* The number of nodes is nodes;
10+
* The value of the i-th node is value[i];
11+
* The parent of the i-th node is parent[i].
12+
* Remove every subtree whose sum of values of nodes is zero.
13+
* After doing so, return the number of nodes remaining in the tree.
14+
*
15+
* Example 1:
16+
* 0 (1)
17+
* / \
18+
* (-2) 1 2 (4)
19+
* / / | \
20+
* (0)3 (-2)4 (-1)5 6(-1)
21+
*
22+
* Input: nodes = 7, parent = [-1,0,0,1,2,2,2], value = [1,-2,4,0,-2,-1,-1]
23+
* Output: 2
24+
*
25+
* Constraints:
26+
* 1 <= nodes <= 10^4
27+
* -10^5 <= value[i] <= 10^5
28+
* parent.length == nodes
29+
* parent[0] == -1 which indicates that 0 is the root.
30+
* */
31+
publicclass_1273 {
32+
publicstaticclassSolution1 {
33+
publicintdeleteTreeNodes(intnodes,int[]parent,int[]value) {
34+
intlen =parent.length;
35+
for (inti =len -1;i >0; ) {
36+
intsum =0;
37+
intparentIndex =parent[i];
38+
while (i >0 &&parent[i] ==parentIndex) {
39+
sum +=value[i--];
40+
}
41+
value[parentIndex] =value[parentIndex] +sum;
42+
}
43+
for (inti =0;i <value.length;i++) {
44+
if (value[i] ==0) {
45+
for (intj =0;j <parent.length;j++) {
46+
if (parent[j] ==i) {
47+
value[j] =0;
48+
}
49+
}
50+
}
51+
}
52+
return (int)Arrays.stream(value).filter(i ->i !=0).count();
53+
}
54+
}
55+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1273;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1273Test {
10+
privatestatic_1273.Solution1solution1;
11+
privatestaticint[]parent;
12+
privatestaticint[]value;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_1273.Solution1();
17+
}
18+
19+
@Test
20+
publicvoidtest1() {
21+
parent =newint[]{-1,0,0,1,2,2,2};
22+
value =newint[]{1, -2,4,0, -2, -1, -1};
23+
assertEquals(2,solution1.deleteTreeNodes(7,parent,value));
24+
}
25+
26+
@Test
27+
publicvoidtest2() {
28+
parent =newint[]{-1,0,0,1,2,2,2};
29+
value =newint[]{1, -2,3,0, -2, -1,0};
30+
assertEquals(2,solution1.deleteTreeNodes(7,parent,value));
31+
}
32+
33+
@Test
34+
publicvoidtest3() {
35+
parent =newint[]{-1,0,0,1,2,2,2};
36+
value =newint[]{1, -2,4,0, -2, -1, -2};
37+
assertEquals(6,solution1.deleteTreeNodes(7,parent,value));
38+
}
39+
40+
@Test
41+
publicvoidtest4() {
42+
parent =newint[]{-1,0,0,1,2,2,2};
43+
value =newint[]{3, -2,4,0, -2, -1, -2};
44+
assertEquals(0,solution1.deleteTreeNodes(7,parent,value));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp