310. Minimum Height Trees

Strip off the tree layer by layer from outside.

import java.lang.reflect.Array;
public class Solution {
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        if(n==0) return new ArrayList<Integer>();
        if(n==1) return new ArrayList<Integer>(){{add(0);}};
        
        ArrayList<Integer>[] adjencencyList = (ArrayList<Integer>[]) Array.newInstance(ArrayList.class, n);
        for(int i = 0; i<n; ++i)
        {
            adjencencyList[i] = new ArrayList<Integer>();
        }
        int[] degrees = new int[n];
        for(int[] edge: edges)
        {
            adjencencyList[edge[0]].add(edge[1]);
            ++degrees[edge[0]];
            adjencencyList[edge[1]].add(edge[0]);
            ++degrees[edge[1]];
        }
        
        ArrayList<Integer> outmostLayer = new ArrayList<Integer>();
        for(int i=0; i<n; ++i)
        {
            if(degrees[i]==1)
                outmostLayer.add(i);
        }
        
        while(n>2) //have either 1 or 2 MHTs.
        {
            ArrayList<Integer> strip = new ArrayList<Integer>();
            for(int leaf: outmostLayer)
            {
                --n;
                --degrees[leaf];
                for(int neighbor: adjencencyList[leaf])
                {
                    --degrees[neighbor];
                    if(degrees[neighbor] == 1)
                        strip.add(neighbor);
                }
            }
            outmostLayer = strip;
        }
        
        return outmostLayer;
    }
}

 

 

This version calculates the height for each node, and finds the minimum height among all. It fails with Time Limit Exceeded:

import java.lang.reflect.Array;
public class Solution {
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        List<Integer> results = new ArrayList<Integer>();
        if(n == 0)
            return results;
        
        ArrayList<Integer>[] adjencencyList = (ArrayList<Integer>[]) Array.newInstance(ArrayList.class, n);
        for(int i = 0; i<n; ++i)
        {
            adjencencyList[i] = new ArrayList<Integer>();
        }
        for(int[] edge: edges)
        {
            adjencencyList[edge[0]].add(edge[1]);
            adjencencyList[edge[1]].add(edge[0]);
        }
        
        Integer[] heightArray = new Integer[n];
        
        for(int i = 0; i<n; ++i)
        {
            boolean[] visited = new boolean[n];
            visited[i] = true;
            ArrayList<Integer> level = adjencencyList[i];
            int height = 0;
            while(!level.isEmpty())
            {
                ++ height;
                ArrayList<Integer> newLevel = new ArrayList<Integer>();
                for(Integer node : level)
                {
                  if(!visited[node])
                  {
                    visited[node] = true;
                    for(Integer nn: adjencencyList[node])
                    {
                      if(!visited[nn])
                        newLevel.add(nn);
                    }
                  }
                }
                level = newLevel;
            }
            heightArray[i] = height;
        }
        
        Integer minimumHeight = Integer.MAX_VALUE;
        for(int i=0;i<n;++i)
        {
            Integer h = heightArray[i];
            if(h<minimumHeight)
            {
                minimumHeight = h;
                results.clear();
            }
            
            if(h == minimumHeight) 
            {
                results.add(i);
            }
        }
        
        return results;
    }
}

 

你可能感兴趣的:(310. Minimum Height Trees)