Simple data structure but not that easy to figure out.. MHT -> balanced tree.
https://leetcode.com/problems/minimum-height-trees/

Lesson learnt: Focus on problem itself. Play with it. Do NOT jam your brain with knowledge!

class Solution
{
public:
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges)
{
vector<int> ret;
int m = edges.size();
if(!m) return {}; // Set up Graph
unordered_set<int> leaves;
unordered_set<int> all;
for(int i = ; i < n; i ++)
{
leaves.insert(i);
all.insert(i);
}
if(all.size() < )
{
for(auto v: all) ret.push_back(v);
return ret;
} unordered_map<int, unordered_set<int>> g;
for(auto &p : edges)
{
g[p.first].insert(p.second);
if(g[p.first].size() > )
leaves.erase(p.first);
g[p.second].insert(p.first);
if(g[p.second].size() > )
leaves.erase(p.second);
} queue<int> q;
for(auto l : leaves)
q.push(l); unordered_set<int> cs;
while(!q.empty())
{
int v = q.front(); q.pop();
all.erase(v);
for(auto c: g[v])
{
if(all.count(c))
{
g[c].erase(v);
if(g[c].size() == )
cs.insert(c);
}
}
if(q.empty())
{
if(all.size() <= ) break; for(auto v : cs) q.push(v);
cs.clear();
}
}
for(auto v: all) ret.push_back(v);
return ret;
}
};

LeetCode "Minimum Height Tree" !!的更多相关文章

  1. [LeetCode] Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  2. LeetCode Minimum Height Trees

    原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ...

  3. [LeetCode] 310. Minimum Height Trees 解题思路

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  4. [LeetCode] 310. Minimum Height Trees 最小高度树

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  5. 【LeetCode】310. Minimum Height Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...

  6. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  7. leetcode@ [310] Minimum Height Trees

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  8. [LeetCode] 310. Minimum Height Trees_Medium tag: BFS

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  9. Minimum Height Trees -- LeetCode

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

随机推荐

  1. mark资料-python编辑器的选择与安装

    1.pycharm 下载地址: 注册码: 注意事项: 2.ulipad参考虫师的地址 url: 3.eclipse+pydev

  2. vcf_filter.py

    pyvcf 中带的一个工具 比其他工具用着好些 其他filter我很信不过~~  自己写的功能又很有限 所以转投vcf_filter.py啦 Filtering a VCF file based on ...

  3. Linux下删除乱码或特殊字符文件

    今天遇到一个问题,一个文件名是“-MXV9.log”,直接用rm删除的时候就报错,如下: [localhost]rm -MXV9.log rm: illegal option -- M rm: ill ...

  4. C语言学习笔记 -冒泡排序

    //冒泡排序 void main(){ , , , , }; ]); ; i<max - ; i++) { for (int j = i; j<max; j++) { if (a[i]&g ...

  5. leetcode之反转链表

    原文链接:点击打开链接 Reverse a singly linked list A linked list can be reversed either iteratively or recursi ...

  6. Sql Server_笔记

    1.随机取出10条数据:select top 10 * from tablename order by newid()

  7. JavaWeb学习记录(九)——Cookie的增加、删除、查看

    一.servlet功能代码: public void doGet(HttpServletRequest request, HttpServletResponse response)           ...

  8. ntpdate:no server suitable for synchronization found

    Question: 在使用ntpdate同步时间时,出现了no server suitable for synchronization found的报错. 通过ntpdate -d s2m.time. ...

  9. IOS中使用手机号注册

    #import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface KCVVerify : NSObject ...

  10. 重学OpenGL(一)----工具篇

    最近想开发一个小工具,需要用到3D,果断上OpenGL,借这个过程把OpenGL重学一遍. 工欲善其事,必先利其器,先把工具都搞好. [开发语言] 果断C+OpenGL,不解释. [开发环境] Min ...