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 ...
随机推荐
- isFinite
window.isFinite 如果 number 是有限数字(或可转换为有限数字),那么返回 true.否则,如果 number 是 NaN(非数字),或者是正.负无穷大的数,则返回 false
- Qt简介
一.Qt与Qt Creator简介 Qt是一个跨平台应用程序和 UI 开发框架.使用 Qt 您只需一次性开发应用程序,无须重新编写源代码,便可跨不同桌面和嵌入式操作系统部署这些应用程序. ...
- WeCenter程序安装
WeCenter程序安装时需要GD库和freetype的支持,以下是安装方法 GD库的安装:我们可以直接使用yum命令来安装,自动解决依赖关系及安装GD库相关的包. [root@localhost ~ ...
- UVa 10900 - So you want to be a 2n-aire?
题目大意: 一个答题赢奖金的问题,玩家初始的金额为1,给出n,表示有n道题目,t表示说答对一道题目的概率在t到1之间,每次面对一道题,可以选择结束游戏,获得当前奖金:回答下一道问题,答对的概率p在t到 ...
- 225. Implement Stack using Queues
代码如下: class MyStack { // Push element x onto stack. Queue<Integer> queue=new ArrayDeque<> ...
- 安装arbotix simulator仿真环境--9
原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 周学伟 安装之前:首先确保已经正常制作了ros工作空间并且安装了rbx1功能包: cd ~/catki ...
- scala言语基础学习九
模式匹配 case _ =>不能放在函数的中间必须放在最后,否则scala会编译不通过 在case 里面使用if守卫 在模式匹配中获取输入的数据(在匹配不到的情况下) 对类型进行匹配 case ...
- 使用PHP的curl扩展实现跨域post请求,以及file_get_contents()百度短网址例子
<?php $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php"); curl_se ...
- A better SHOW TABLE STATUS
From command line we have the entire MySQL server on hands (if we have privileges too of course) but ...
- LVM磁盘管理
http://www.cnblogs.com/gaojun/archive/2012/08/22/2650229.html Linux LVM硬盘管理及LVM扩容 LVM磁盘管理 一.LVM简介... ...