leetcode.310 minimum tree height

For the tree without having to FIG feature, we can select any node as the root. FIG tree can become so in all possible tree, the tree having a minimum height of the tree is called the minimum height. Given such a map, write a function to find all of the minimum height of the tree and return to their root.

format

This figure comprises n nodes, labeled 0 to n - 1. The given number n to the side edges and a free list (each side is a pair tag).

You can assume that there are no duplicate will appear in the edges of the side. Since all edges are undirected edges, [0, 1] and [1, 0] is the same, and therefore do not occur simultaneously in the edges.

Example 1:

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

0
|
1
/ \
2 3

Output: [1]
Example 2:

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

0 1 2
\ | /
3
|
4
|
5

Output: [3, 4]
Description:

 According to the definition of the tree, the tree is an undirected graph, wherein any two vertices are connected by only one path. In other words, a simple loop without any communication with the FIG are a tree.
Height of the tree is the number of the longest path between the root node down the upper side and the leaf nodes.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/minimum-height-trees
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

The first attempt is to use a depth-first algorithm to calculate the height of each point as a root, and then find the minimum value, the result is too large time complexity. (I also want to know how much)

The second, multiple leaf nodes may be removed, i.e., 1 degree point. When found after a removal of all the points have been removed, the removal point is the answer. But each time removing the same time be careful not to judge simultaneous removal, that would lead to sub-leaf nodes are removed in the same round. Which points need to be removed should first record the removal and re-unified border erase.

class Solution {
public:
    int visited[100000],deg[100000];
/*    int dfs(int t,int heigh, vector<vector<int>>& edges){
        int max=-1,o,i;
        for(i=0;i<edges.size();i++){
            if(edges[i][0]==t&&visited[edges[i][1]]==0){
                visited[edges[i][1]]=1;
                o=dfs(edges[i][1],heigh+1,edges);
                if(o>max) max=o;
                visited[edges[i][1]]=0;
            }else if(edges[i][1]==t&&visited[edges[i][0]]==0){
                visited[edges[i][0]]=1;
                o=dfs(edges[i][0],heigh+1,edges);
                if(o>max) max=o;
                visited[edges[i][0]]=0;
            }
        }
        if(max==-1){
            return heigh;
        }
        return max;
    }
    int height(int k,int n, vector<vector<int>>& edges){

        for(i=0;i<n;i++){
            visited[i]=0;
        }

    }*/
    /*vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
        int i, ii;
        for(i=0;i<n;i++){
            visited[i]=0;
        }
        vector<int> res;
        you r, min = 10000000;
        for(i=0;i<n;i++){
            visited[i]=1;
            r=dfs(i,0,edges);
            if(r<min){
                res.clear();
                res.push_back(i);
                my = r;
            }else if(r==min){
                res.push_back(i);
            }
            //cout<<i<<"  "<<r<<endl;
            visited[i]=0;
        }
        return res;
    }*/
        vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {
            int i;
            int live[n];
            for(i=0;i<n;i++){
                you [i] = 0 ;
                live[i]=1;
            }
            for(i=0;i<edges.size();i++){
                you [edges [i] [ 0 ]] ++ ;
                you [edges [i] [ 1 ]] ++ ;
            }
            vector<int> res;
            int t=1,j,z=n;
            while(z>0){
                res.clear();
                for(i=0;i<n;i++){
                    if(deg[i]<=t&&live[i]==1){
                        res.push_back(i);
                        }
                    }
                        for(i=0;i<res.size();i++){
                        for(j=0;j<edges.size();j++){
                            if(edges[j][0]==res[i]&&live[edges[j][1]]==1){
                                provides [edges [j] [ 1 ]] - ;
                            }
                            if(edges[j][1]==res[i]&&live[edges[j][0]]==1){
                                provides [edges [j] [ 0 ]] - ;
                            }
                            }
                    with - ;
                    live[res[i]]=0;
                    //cout<<"t="<<t<<"  "<<i<<" is moved\n";
                        }
                }
            return res;
        }
};

Guess you like

Origin www.cnblogs.com/hyffff/p/12152353.html