-
Notifications
You must be signed in to change notification settings - Fork 5
/
CountNodesWithTheHighestScore.java
131 lines (123 loc) · 3.41 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*https://leetcode.com/problems/count-nodes-with-the-highest-score/*/
class TreeNode
{
TreeNode left;
long leftSubtree;
int data;
long rightSubtree;
TreeNode right;
TreeNode(int data)
{
this.data = data;
}
}
class Solution {
TreeNode root;
long totalNodes;
int maxCount;
long max;
public int countHighestScoreNodes(int[] parents) {
int i, n = parents.length;;
max = 0l;
maxCount = 0;
TreeNode curr, par;
Map<Integer,TreeNode> map = new HashMap<Integer,TreeNode>();
for (i = 0; i < n; ++i)
map.put(i,new TreeNode(i));
for (i = 1; i < n; ++i)
{
curr = map.get(i);
par = map.get(parents[i]);
if (par.left == null) par.left = curr;
else par.right = curr;
}
root = map.get(0);
totalNodes = count(root);
findMax(root);
// System.out.println(max);
return maxCount;
}
private long count(TreeNode root)
{
if (root == null) return 0;
root.leftSubtree = count(root.left);
root.rightSubtree = count(root.right);
return root.leftSubtree+root.rightSubtree+1;
}
private void findMax(TreeNode root)
{
if (root == null) return;
long rem = totalNodes-(root.leftSubtree+root.rightSubtree+1);
long result = (rem == 0 ? 1 : rem)*(root.leftSubtree == 0 ? 1 : root.leftSubtree)*(root.rightSubtree == 0 ? 1 : root.rightSubtree);
// System.out.println(root.data+" "+result);
if (result > max)
{
max = result;
maxCount = 1;
} else if (result == max)
++maxCount;
findMax(root.left);
findMax(root.right);
}
}
class TreeNode
{
TreeNode left;
long leftSubtree;
int data;
long rightSubtree;
TreeNode right;
TreeNode(int data)
{
this.data = data;
}
}
class Solution {
TreeNode root;
long totalNodes;
int maxCount;
long max;
public int countHighestScoreNodes(int[] parents) {
int i, n = parents.length;;
max = 0l;
maxCount = 0;
TreeNode curr, par;
TreeNode[] map = new TreeNode[n];
for (i = 0; i < n; ++i)
map[i] = new TreeNode(i);
for (i = 1; i < n; ++i)
{
curr = map[i];
par = map[parents[i]];
if (par.left == null) par.left = curr;
else par.right = curr;
}
root = map[0];
totalNodes = count(root);
findMax(root);
// System.out.println(max);
return maxCount;
}
private long count(TreeNode root)
{
if (root == null) return 0;
root.leftSubtree = count(root.left);
root.rightSubtree = count(root.right);
return root.leftSubtree+root.rightSubtree+1;
}
private void findMax(TreeNode root)
{
if (root == null) return;
long rem = totalNodes-(root.leftSubtree+root.rightSubtree+1);
long result = (rem == 0 ? 1 : rem)*(root.leftSubtree == 0 ? 1 : root.leftSubtree)*(root.rightSubtree == 0 ? 1 : root.rightSubtree);
// System.out.println(root.data+" "+result);
if (result > max)
{
max = result;
maxCount = 1;
} else if (result == max)
++maxCount;
findMax(root.left);
findMax(root.right);
}
}