Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2220793

Browse files
add 1448
1 parent 53230e5 commit 2220793

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1448|[Count Good Nodes in Binary Tree](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1448.java) | |Medium|Tree, DFS|
1112
|1447|[Simplified Fractions](https://leetcode.com/problems/simplified-fractions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1447.java) | |Medium|Math|
1213
|1446|[Consecutive Characters](https://leetcode.com/problems/consecutive-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1446.java) | |Easy|String|
1314
|1441|[Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1441.java) | |Easy|Stack|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.Collections;
6+
import java.util.PriorityQueue;
7+
8+
public class _1448 {
9+
public static class Solution1 {
10+
int count;
11+
12+
public int goodNodes(TreeNode root) {
13+
dfs(root, new PriorityQueue<>(Collections.reverseOrder()));
14+
return count;
15+
}
16+
17+
private void dfs(TreeNode root, PriorityQueue<Integer> maxHeap) {
18+
if (root == null) {
19+
return;
20+
}
21+
maxHeap.offer(root.val);
22+
if (root.val >= maxHeap.peek()) {
23+
count++;
24+
}
25+
if (root.left != null) {
26+
dfs(root.left, maxHeap);
27+
}
28+
if (root.right != null) {
29+
dfs(root.right, maxHeap);
30+
}
31+
maxHeap.remove(root.val);
32+
}
33+
}
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1448;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1448Test {
13+
private static _1448.Solution1 solution1;
14+
private static TreeNode root;
15+
16+
@Test
17+
public void test1() {
18+
solution1 = new _1448.Solution1();
19+
root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, 4, 3, null, 1, 5));
20+
assertEquals(4, solution1.goodNodes(root));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
solution1 = new _1448.Solution1();
26+
root = TreeUtils.constructBinaryTree(Arrays.asList(3, 3, null, 4, 2));
27+
assertEquals(3, solution1.goodNodes(root));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
solution1 = new _1448.Solution1();
33+
root = TreeUtils.constructBinaryTree(Arrays.asList(1));
34+
assertEquals(1, solution1.goodNodes(root));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
solution1 = new _1448.Solution1();
40+
root = TreeUtils.constructBinaryTree(Arrays.asList(2, null, 4, 10, 8, null, null, 4));
41+
assertEquals(4, solution1.goodNodes(root));
42+
}
43+
44+
@Test
45+
public void test5() {
46+
solution1 = new _1448.Solution1();
47+
root = TreeUtils.constructBinaryTree(Arrays.asList(9, null, 3, 6));
48+
assertEquals(1, solution1.goodNodes(root));
49+
}
50+
51+
}

0 commit comments

Comments
 (0)