-
Notifications
You must be signed in to change notification settings - Fork 7
/
GoodNodeCounter.java
54 lines (40 loc) · 1.48 KB
/
GoodNodeCounter.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package binary_tree_dfs;
public class GoodNodeCounter {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
private int count = 0;
public int goodNodes(TreeNode root) {
if (root == null) return 0;
dfs(root, root.val);
return count;
}
private void dfs(TreeNode node, int maxSoFar) {
if (node == null) return;
if (node.val >= maxSoFar) count++;
dfs(node.left, Math.max(maxSoFar, node.val));
dfs(node.right, Math.max(maxSoFar, node.val));
}
private static TreeNode constructTree(Integer[] arr, int i) {
if (i >= arr.length || arr[i] == null) return null;
TreeNode root = new TreeNode(arr[i]);
root.left = constructTree(arr, 2 * i + 1);
root.right = constructTree(arr, 2 * i + 2);
return root;
}
public static void main(String[] args) {
GoodNodeCounter counter = new GoodNodeCounter();
TreeNode root1 = constructTree(new Integer[]{3,1,4,3,null,1,5}, 0);
assert counter.goodNodes(root1) == 4 : "Test case 1 failed";
TreeNode root2 = constructTree(new Integer[]{3,3,null,4,2}, 0);
assert counter.goodNodes(root2) == 3 : "Test case 2 failed";
TreeNode root3 = constructTree(new Integer[]{1}, 0);
assert counter.goodNodes(root3) == 1 : "Test case 3 failed";
System.out.println("All test cases passed!");
}
}