Leetcode 310:minimum Height Trees

Source: Internet
Author: User

For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height is called minimum height trees (mhts). Given such a graph, write a function to find all the mhts and return a list of their root labels.

Format
The graph contains n nodes which is labeled from 0 to n - 1 . You'll be given the number and n a list of undirected edges (each edge is a pair of labels).

You can assume that no duplicate edges would appear in edges . Since all edges was undirected, is the same as and thus would not [0, 1] [1, 0] appear together in edges .

Example 1:

Given n = 4 ,edges = [[1, 0], [1, 2], [1, 3]]

        0        |        1       /       2   3

Return[1]

Example 2:

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

     0  1  2      \ |/        3        |        4        |        5

Return[3, 4]

Note:

(1) According to the definition of the tree on Wikipedia: "A tree was an undirected graph in which any and vertices is connect Ed by exactly one path. In the other words, any connected graph without simple cycles is a tree. "

(2) The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

Ideas:

(Initial idea: At any point as the root node, then breadth-first traversal graph, find the minimum depth, time complexity O (n (n+e)), the result timeout)

Post Reference http://www.cnblogs.com/grandyang/p/5000291.html

Starting from the leaf node, the first layer is deep into the center, and the last remaining nodes (less than two) are the result sets;

Note that there is only one point in the situation;

1 classSolution {2  Public:3vector<int> Findminheighttrees (intN, vector<pair<int,int>>&edges) {4vector<vector<int>> g (n,vector<int>());5vector<int> d (N,0);6         intLen =edges.size ();7          for(intI=0; i<len;i++)8         {9             intA =Edges[i].first;Ten             intb =Edges[i].second; One G[a].push_back (b); A G[b].push_back (a); -d[a]++; -d[b]++; the         } -queue<int>Q; -          for(intI=0; i<n;i++) -         { +             if(d[i]==1) - Q.push (i); +         } A          while(n>2) at         { -             intLen2 =q.size (); -              for(intI=0; i<len2;i++) -             { -n--; -                  intt =Q.front (); in Q.pop (); -                   for(intj=0; J<g[t].size (); j + +) to                  { +d[g[t][j]]--; -                      if(d[g[t][j]]==1) the Q.push (G[t][j]); *                  } $             }Panax Notoginseng         } -vector<int>ans; the          while(!q.empty ()) +         { A Ans.push_back (Q.front ()); the Q.pop (); +         } -         if(ans.size () = =0) $Ans.push_back (0); $         returnans; -     } -};
View Code

Leetcode 310:minimum Height Trees

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.