-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCountOfGoodNodes.java
56 lines (47 loc) · 1.54 KB
/
CountOfGoodNodes.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
55
56
package main.java.topcodingquestion.treesandgraphs;
import java.util.LinkedList;
import java.util.Queue;
public class CountOfGoodNodes {
public int goodNodesII(TreeNode root) {
Queue<TreeNodePair> queue = new LinkedList<>();
int ans = 0;
queue.add(new TreeNodePair(root, Integer.MIN_VALUE));
while (!queue.isEmpty()) {
TreeNodePair tp = queue.poll();
if (tp.node.val >= tp.val) {
ans += 1;
}
if (tp.node.left != null) {
queue.add(new TreeNodePair(tp.node.left, Math.max(tp.val, tp.node.val)));
}
if (tp.node.right != null) {
queue.add(new TreeNodePair(tp.node.right, Math.max(tp.val, tp.node.val)));
}
}
return ans;
}
public int goodNodesI(TreeNode root) {
if (root == null) {
return 0;
}
int[] count = new int[1];
return helper(root, Integer.MIN_VALUE, count);
}
private int helper(TreeNode tree, int max, int[] count) {
if (tree == null) {
return 0;
}
count[0] = tree.val >= max ? 1 : 0;
count[0] += helper(tree.left, Math.max(max, tree.val), count);
count[0] += helper(tree.right, Math.max(max, tree.val), count);
return count[0];
}
class TreeNodePair {
TreeNode node;
int val;
TreeNodePair(TreeNode node, int val) {
this.node = node;
this.val = val;//track of maxval till parent node
}
}
}