File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed
src/main/kotlin/g3501_3600/s3585_find_weighted_median_node_in_tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change @@ -21,9 +21,9 @@ class Solution {
21
21
} else {
22
22
longMax = 1
23
23
}
24
- adj = ArrayList < MutableList < IntArray >> ()
24
+ adj = ArrayList ()
25
25
for (i in 0 .. < n) {
26
- adj.add(ArrayList < IntArray > ())
26
+ adj.add(ArrayList ())
27
27
}
28
28
for (edge in edges) {
29
29
val u = edge[0 ]
@@ -34,7 +34,7 @@ class Solution {
34
34
}
35
35
depth = IntArray (n)
36
36
dist = LongArray (n)
37
- parent = Array < IntArray > (longMax) { IntArray (n) }
37
+ parent = Array (longMax) { IntArray (n) }
38
38
for (i in 0 .. < longMax) {
39
39
parent[i].fill(- 1 )
40
40
}
@@ -117,15 +117,15 @@ class Solution {
117
117
val lca = getLCA(u, v)
118
118
val totalPathWeight = dist[u] + dist[v] - 2 * dist[lca]
119
119
val halfWeight = (totalPathWeight + 1 ) / 2L
120
- if (dist[u] - dist[lca] >= halfWeight) {
120
+ return if (dist[u] - dist[lca] >= halfWeight) {
121
121
var curr = u
122
122
for (p in longMax - 1 downTo 0 ) {
123
123
val nextNode = parent[p][curr]
124
124
if (nextNode != - 1 && (dist[u] - dist[nextNode] < halfWeight)) {
125
125
curr = nextNode
126
126
}
127
127
}
128
- return parent[0 ][curr]
128
+ parent[0 ][curr]
129
129
} else {
130
130
val remainingWeightFromLCA = halfWeight - (dist[u] - dist[lca])
131
131
var curr = v
@@ -137,7 +137,7 @@ class Solution {
137
137
curr = nextNode
138
138
}
139
139
}
140
- return curr
140
+ curr
141
141
}
142
142
}
143
143
}
You can’t perform that action at this time.
0 commit comments