LeetCode319 Minimum height trees

we have an undirected graph, we can choose any node as root. now, we wantt to choose the node as root which makes the tree with minimun height. and the tree desn;t have to be binary as long as it is a tree. if we have multiple root all minimum height, then return them all.

Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]

    0
    |
    1
   / \
  2   3 

Output: [1]

clearly, if we choose 1 as the root of tree, the height of the tree is minumum.

first, we use the bfs way:

//we bfs for everynode, and keep track of the minimum length
class Solution {
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        if (n == 1) return Collections.singletonList(0);

        List<Set<Integer>> adj = new ArrayList<>(n); //use n as the parameter in this list, this list is just like a hashmap, with index as the key and set as value. why do we have to use n at first? becsue this is likr a hashmap, if you do not create a key first,and arrange space for that, you have to deal with them later.
        for (int i = 0; i < n; ++i) adj.add(new HashSet<>()); //initialize it one by one
        for (int[] edge : edges) {
            adj.get(edge[0]).add(edge[1]);
            adj.get(edge[1]).add(edge[0]);
        } //and put every edge in adj

        List<Integer> leaves = new ArrayList<>(); //use a list to record every leaf
        for (int i = 0; i < n; ++i)
            if (adj.get(i).size() == 1) leaves.add(i); //leave is the nodes that linked with only one other node. think about it, why? because if one node linked two or more, then it can't be child node because each child node can only have one parent, not two or more

        while (n > 2) { //when there is only 1 or 2 nodes left
            n -= leaves.size();
            List<Integer> newLeaves = new ArrayList<>();
            for (int i : leaves) { //for every node in leaves
                int j = adj.get(i).iterator().next(); //for everything that linked to current node i
                adj.get(j).remove(i); //in the mean time, remove the i node in j
                if (adj.get(j).size() == 1) newLeaves.add(j); //if the j node becomes the leaf node
            }
            leaves = newLeaves; //update leaves as newLeaves for the next round of while loop
        }//in general, each of this while loop, one layer of the leaf node will be elimnated and new leaf node will appear. until there the total node remaining is less or equals to 2.  
        
        return leaves;
    }

// Runtime: 53 ms
}

the general idea of this code is: starting from the first layer of all the leaf node. we elimate all of them, and then, the second layer of leaf nodes…until there is less than or equals to 2 nodes that hasnot been elmiated. then the last layer of nodes that’s been eliminated before is the center points where the root should be placed in order to make height of tree minimum.
even though it seems that its easy to understand, but in detail, I really don’t think I really understad this"the last layer is the final results".

posted @ 2020-05-08 11:01  EvanMeetTheWorld  阅读(16)  评论(0)    收藏  举报