# Where am I wasting time here

• Hi,

I came with this solution based on BFS. I believe it works for small cases but run out of time for large trees. Obviously I'm doing superfluous work but I'm not sure where. Any help would be appreciated.

public class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> winners = new ArrayList<>();
int minDepth = Integer.MAX_VALUE;

for(int i = 0; i < links.length; i++) links[i] = new HashSet<>();

for(int[] edge : edges) {
}

for(int i = 0; i < n; ++i) {
int mhtHeight = findMinHeightTree(i, n, links);

if(mhtHeight == minDepth) {
} else if(mhtHeight < minDepth) {
winners.clear();
minDepth = mhtHeight;
}

}

return winners;
}

public Integer findMinHeightTree(int root, int n, HashSet<Integer>[] edges) {
int minDepth = Integer.MAX_VALUE;

Set<Integer> markedNode = new HashSet<>();
Queue<Integer> q = new ArrayDeque<>();
Map<Integer,Integer> depths = new TreeMap<>();

depths.put(root, 0);

while(!q.isEmpty()) {
int node = q.remove();

boolean isLeaf = true;
for(int i = 0; i < n; i ++) {
if(edges[node].contains(i) && !markedNode.contains(i)) {
isLeaf = false;
int depth = depths.get(node) + 1;
depths.put(i, depth);

if(depth < minDepth)
}
}

if(isLeaf) {
int depth = depths.get(node);
if(depth < minDepth) minDepth = depth;
}
}

return minDepth;
}

• bhai help mile to hume bhi batana

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.