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 ...
随机推荐
- Open vSwitch简述
一.基础术语 1.Packet (数据包):网络转发的最小数据单元,每个包都来自某个端口,最终会被发往一个或多个目标端口,转发数据包的过程就是网络的唯一功能. 2.Bridge (网桥):Open v ...
- html5实现饼图和线图-我们到底能走多远系列(34)
我们到底能走多远系列(34) 扯淡: 送给各位一段话: 人生是一个不断做加法的过程 从赤条条无牵无挂的来 到学会荣辱羞耻 礼仪规范 再到赚取世间的名声 财富 地位 ...
- 用python做爬虫的例子
主要就是用了两个库,urllib和BeautifulSoup. 作用是从HTML中解析出解梦的查询词和具体的解释. # -*- coding: utf-8 -*- import urllib, url ...
- 使用isInEditMode解决可视化编辑器无法识别自定义控件的问题
如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码, 会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to s ...
- 怎么设置session无响应超时时间并且自动返回登陆页面
------解决方案--------------------看下我的 session.setAttribute(UserInfo.USERINFO, userinfo); session.setMax ...
- AttributeError: 'NoneType' object has no attribute 'bytes' python3.4 win64
解决方法: easy_install -U pip 详见: https://github.com/pypa/pip/issues/2669
- 关于ZF2中一点感悟,service_manager
在zf2中,在serviceLoctor中自定义的内容,可以通$serviceLocator->get('config')['key'],如果是在serivce_manger中定义的服务名,其实 ...
- hihoCoder #1033 : 交错和 (数位Dp)
题目大意: 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1,定义交错和函数: f(x) = a0 - a1 + a2 - ... + ( - 1)n - ...
- CentOS配置LAMP环境
环境:CentOS 6.5 配置防火墙,开启80端口.3306端口 # Firewall configuration written by system-config-firewall # Manua ...
- 越狱Season 1-Episode 21: Go
Season 1, Episode 21: Go -Michael: I need you to let me get us out of here. 我需要你帮我出去 -Patoshik: If y ...