LeetCode "Minimum Height Tree" !!
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" !!的更多相关文章
- [LeetCode] Minimum Height Trees 最小高度树
		For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ... 
- LeetCode Minimum Height Trees
		原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ... 
- [LeetCode] 310. Minimum Height Trees 解题思路
		For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ... 
- [LeetCode] 310. Minimum Height Trees 最小高度树
		For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ... 
- 【LeetCode】310. Minimum Height Trees 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ... 
- 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 ... 
- leetcode@ [310] Minimum Height Trees
		For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ... 
- [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 ... 
- Minimum Height Trees -- LeetCode
		For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ... 
随机推荐
- 博客Mac桌面编辑器-cnblogs
			Mac篇 公司的机器内存只有8G,不想再大动干戈为了Windows Live Writer装个Vmware了,谷歌娘讲MarsEdit不错,那就试试用这个写个试用贴呗 就是这货了,果然是火星来的, ... 
- java apache commons HttpClient发送get和post请求的学习整理(转)
			文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ... 
- 用python处理数学问题
			一, 计算对数: >>> import math #导入数学模块>>> math.log(8,2) #计算以2为底 8的对数3.0>&g ... 
- DB2 函数大全
			DB2函数大全 函数名 函数解释 函数举例 AVG() 返回一组数值的平均值. SELECTAVG(SALARY)FROMBSEMPMS; CORR(),CORRELATION() 返回一对数值的关系 ... 
- ZOJ1238 Guess the Number
			/*In this problems, we’ll talk about BIG numbers. Yes, I’m sorry, big numbers again…. Let N be a pos ... 
- jquery获取第几个元素的方法总结
			使用jquery时经常会遇到,选择器选择一组元素后,需要在这组元素中找到第几个元素. jquery中使用eq()方法找到第几个元素或第N个元素,jquery中eq()的使用如下: eq() 选择器选取 ... 
- 资源预加载  Preload
			当提到前端性能优化时,我们首先会联想到文件的合并.压缩,文件缓存和开启服务器端的 gzip 压缩等,这使得页面加载更快,用户可以尽快使用我们的 Web 应用来达到他们的目标. 资源预加载 是另一个性能 ... 
- leetcode 102  Binary Tree Level Order Traversal ----- java
			Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ... 
- 强化学习之 免模型学习(model-free based learning)
			强化学习之 免模型学习(model-free based learning) ------ 蒙特卡罗强化学习 与 时序查分学习 ------ 部分节选自周志华老师的教材<机器学习> 由于现 ... 
- rac    ASM下最简单归档开启方法
			原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/47172639本次先 ... 
