forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountNodesWiththeHighestScore.java
73 lines (57 loc) · 1.37 KB
/
CountNodesWiththeHighestScore.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution {
class TreeNode {
private TreeNode left;
private TreeNode right;
private long score = 0L;
private int nodeCount = 0;
}
private int countNodes (TreeNode root) {
if (root == null) {
return 0;
}
root.nodeCount = countNodes(root.left) + countNodes(root.right) + 1;
return root.nodeCount;
}
public int countHighestScoreNodes(int[] parents) {
long max = 0l;
int n = parents.length;
int ans = 0;
TreeNode[] tree = new TreeNode[n];
for (int i = 0; i < parents.length; i++) {
tree[i] = new TreeNode();
}
for (int i = 1; i < parents.length; i++) {
int parentId = parents[i];
if (tree[parentId].left == null) {
tree[parentId].left = tree[i];
} else {
tree[parentId].right = tree[i];
}
}
countNodes(tree[0]);
for (int i = 0; i < parents.length; i++) {
long product = 1;
int leftCnt = tree[i].left == null ? 0 : tree[i].left.nodeCount;
int rightCnt = tree[i].right == null ? 0 : tree[i].right.nodeCount;
int parentCnt = n - 1 - leftCnt - rightCnt;
if (leftCnt > 0) {
product *= leftCnt;
}
if (rightCnt > 0) {
product *= rightCnt;
}
if (parentCnt > 0) {
product *= parentCnt;
}
tree[i].score = product;
max = Math.max(max, product);
}
//count nodes
for (int i = 0; i < parents.length; i++) {
if (tree[i].score == max) {
ans++;
}
}
return ans;
}
}