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. JAVA学习之Ecplise IDE 使用技巧(2)第二章:键盘小快手,代码辅助

    上一篇:JAVA学习之Ecplise IDE 使用技巧(1)第一章:我的地盘我做主,工作空间 第二章:键盘小快手,代码辅助 内容包括: 第一:显示行号 如何设置行号:Ecplice菜单Windows& ...

  2. java类型转化之Hbase ImmutableBytesWritable类型转String

    Hbase 的ImmutableBytesWritable类型一般作为RowKey的类型;但也有时候会把值读出来;故有了转化为string一说. ImmutableBytesWritable RowK ...

  3. unable to open sync connection

    有些时候,在你向连接好的Android机器中按照程序时会遭遇失败,logcat提示“unable to open sync connection”. 一般情况下,重启手机几乎肯定可以解决这个问题. 如 ...

  4. 转 SQL Server中关于的checkpoint使用说明

    在SQL Server中有一个非常重要的命令就是CheckPoint,它主要作用是把缓存中的数据写入mdf文件中. 其实在我们进行insert, update, delete时,数据并没有直接写入数据 ...

  5. cdoj 482 优先队列+bfs

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  6. Javascript模块化编程(三):require.js的用法【转】

    作者: 阮一峰 日期: 2012年11月 7日 这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js ...

  7. 弹出层是iframe引入的页面,如果用js进行隐藏呢?

    <script> $(document).ready(function(){ $('.bjfh').click(function(){ parent.document.getElement ...

  8. c 深度剖析 5

    1,指针没有指向一块合法的区域 1指针没有初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h> #include <string. ...

  9. 关于for,while与do while

    Q:输入一个整数i,输出i+(i+1)+...+19+20的结果 S:法1:for #include<stdio.h> #include<math.h> #include< ...

  10. java多线程:并发包中ConcurrentHashMap和jdk的HashMap的对比

    一:HashMap--->底层存储的是Entry<K,V>[]数组--->Entry<K,V>的结构是一个单向的链表static class Entry<K, ...