forked from mehul-1607/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1448.java
34 lines (29 loc) · 888 Bytes
/
_1448.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.Collections;
import java.util.PriorityQueue;
public class _1448 {
public static class Solution1 {
int count;
public int goodNodes(TreeNode root) {
dfs(root, new PriorityQueue<>(Collections.reverseOrder()));
return count;
}
private void dfs(TreeNode root, PriorityQueue<Integer> maxHeap) {
if (root == null) {
return;
}
maxHeap.offer(root.val);
if (root.val >= maxHeap.peek()) {
count++;
}
if (root.left != null) {
dfs(root.left, maxHeap);
}
if (root.right != null) {
dfs(root.right, maxHeap);
}
maxHeap.remove(root.val);
}
}
}