```
public class Solution {
private int maxNode = -1;
private int maxDist = 0;
private ArrayList<Integer>[] adj;
private int[] h1;
private int[] h2;
private int[] prevDep;
private int[] suc;
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> res = new ArrayList<>();
h1 = new int[n];
h2 = new int[n];
prevDep = new int[n];
suc = new int[n];
adj = new ArrayList[n];
if (edges == null || edges.length == 0) {
if (n == 1) {
res.add(0);
}
return res;
}
int i, j;
int cur;
int cnt;
for (int[] e : edges) {
i = e[0];
j = e[1];
if (adj[i] == null) {
adj[i] = new ArrayList<>();
}
if (adj[j] == null) {
adj[j] = new ArrayList<>();
}
adj[i].add(j);
adj[j].add(i);
}
dfs(0);
cur = maxNode;
cnt = h2[maxNode];
while (cnt != (maxDist - 1) / 2) {
cur = suc[cur];
cnt++;
}
res.add(cur);
if (maxDist % 2 == 0) {
res.add(suc[cur]);
}
return res;
}
private void dfs(int n) {
h1[n] = prevDep[n];
for (int next : adj[n]) {
adj[next].remove(Integer.valueOf(n));
prevDep[next] = prevDep[n] + 1;
suc[next] = n;
dfs(next);
int tmpDep = h1[next] == prevDep[next] ? h2[next] + 1 : h1[next] + 1;
if (tmpDep > h1[n]) {
h2[n] = h1[n];
h1[n] = tmpDep;
suc[n] = next;
}
else if (tmpDep > h2[n]) {
h2[n] = tmpDep;
}
}
if (h1[n] + h2[n] + 1 > maxDist) {
maxDist = h1[n] + h2[n] + 1;
maxNode = n;
}
}
}
```

I've tried my best to remove the number of parameters passed into the dfs() method. But it still results in StackOverflowError. But I've seen similar solution that's accepted. Can anyone help? Thx!