leetcode 310. Minimum Height Trees

朴灿烈づ我的快乐病毒、 2022-07-30 14:26 212阅读 0赞

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 are 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 are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).

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

Example 1:

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

  1. 0
  2. |
  3. 1
  4. / \
  5. 2 3

return [1]

Example 2:

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

  1. 0 1 2
  2. \ | /
  3. 3
  4. |
  5. 4
  6. |
  7. 5

return [3, 4]

  1. class Solution {
  2. vector<vector<int>> traverse(int m, map<int, vector<int>>&pairedges,int n,int h)
  3. {
  4. vector<vector<int>>que;
  5. vector<int>visited(n);
  6. vector<int>aa;
  7. aa.push_back(m);
  8. visited[m] = 1;
  9. que.push_back(aa);
  10. while (!que.back().empty())
  11. {
  12. vector<int>bb;
  13. for (int i = 0; i < que.back().size(); i++)
  14. {
  15. for (int j =0;j< pairedges[que.back()[i]].size();j++)
  16. {
  17. if (visited[pairedges[que.back()[i]][j]] == 0)
  18. {
  19. visited[pairedges[que.back()[i]][j]] = 1;
  20. bb.push_back(pairedges[que.back()[i]][j]);
  21. }
  22. }
  23. }
  24. que.push_back(bb);
  25. if (que.size() >= h + 1)
  26. {
  27. if (que.back().empty())
  28. que.pop_back();
  29. return que;
  30. }
  31. }
  32. if (que.back().empty())
  33. que.pop_back();
  34. return que;
  35. }
  36. public:
  37. vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
  38. vector<int>re;
  39. if (edges.empty())
  40. {
  41. re.push_back(0);
  42. return re;
  43. }
  44. map<int, vector<int>>pairedges;
  45. for (int i = 0; i < edges.size(); i++)
  46. {
  47. pairedges[edges[i].first].push_back(edges[i].second);
  48. pairedges[edges[i].second].push_back(edges[i].first);
  49. }
  50. vector<vector<int>>que = traverse(edges[0].first, pairedges,n,n);
  51. int m = que.back()[0];
  52. que.clear();
  53. que = traverse(m, pairedges,n,n);
  54. int minh = que.size()/2+1;
  55. int s = que.size();
  56. if (s % 2 == 0)
  57. {
  58. for (int i = 0; i < que[s / 2 - 1].size(); i++)
  59. {
  60. vector<vector<int>>pp = traverse(que[s / 2 - 1][i], pairedges,n,minh);
  61. if (pp.size() == minh)
  62. {
  63. re.push_back(que[s / 2 - 1][i]);
  64. break;
  65. }
  66. }
  67. for (int i = 0; i < que[s / 2].size(); i++)
  68. {
  69. vector<vector<int>>pp = traverse(que[s / 2][i], pairedges,n,minh);
  70. if (pp.size() == minh)
  71. {
  72. re.push_back(que[s / 2][i]);
  73. return re;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. for (int i = 0; i < que[s / 2].size(); i++)
  80. {
  81. vector<vector<int>>pp = traverse(que[s / 2][i], pairedges,n,minh);
  82. if (pp.size() == minh)
  83. {
  84. re.push_back(que[s / 2][i]);
  85. return re;
  86. }
  87. }
  88. }
  89. return re;
  90. }
  91. };

accept

发表评论

表情:
评论列表 (有 0 条评论,212人围观)

还没有评论,来说两句吧...

相关阅读