[LeetCode] 310. Minimum Height Trees 解题思路
For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.
Format
The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
Example 1:
Given n = 4, edges = [[1, 0], [1, 2], [1, 3]]
0
|
1
/ \
2 3
return [1]
Example 2:
Given n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]
0 1 2
\ | /
3
|
4
|
5
return [3, 4]
Hint:
- How many MHTs can a graph have at most?
Note:
(1) According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”
(2) The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
问题:给定一个拥有树性质的无向图,图的每一个节点都可以视为一棵树的根节点。在所有可能的树中,找出高度最小的树,并返回他们的树根。
思路一 :
求最小高度的树,实际上是一个求最优解题目。求最优解,我首先想到的是动态规划(DP)思路。这个题目也确实满足 DP 的两个主要性质:overlapping substructure & optimal substructure 。思路比较直观:
- 视某个节点为树根节点,求这棵树的树高。
- 对所有节点进行上面的求解,得到 n 个树高,其中高度最小的树的树根即为原题目的解。
对于1,如何求解节点 A 为树根的树高?
假设已知 A 的所有相邻节点分别为树根的各个子树的树高,那么 A根的树高等于 已知的各个子树树高中的最大值 加一。方程式表达如下,即状态转换方程:
height(A) = max(height(A.next0), height(A.next2),... height(A.nextk)) + 1
对于2, 存在大量重复计算。可以借助表格,将已计算的树分支高度保存下来后续使用,避免重复计算。将当前节点以及其中一个相邻节点组合分支方向,求得该分支高度后存入 map<string, int> direc_height 中,其中 direc 有这两个节点组成作为 key ,height 表示高度。
若不使用表格优化,时间复杂度为 O(V*E)。使用表格,相当于进行了剪枝,会快不少。跑 LeetCode 的大集合 V = 1000, E =2000 ,测试耗时是 820ms,可惜没能过 submit 要求。
private:
// dirct consists of two node cur_next, height means the height of the subtree which souce node is the current node and walk forward on the cur_next direction
map<string, int> direc_height;
public:
/**
* calulate the height of a tree which parent node is p and current node is node
*
*/
int getHeight(gnode* node, gnode* p){
int h = ;
for (int i = ; i < node->neighbours.size(); i++){
gnode* tmpn = node->neighbours[i];
if (tmpn == p){
continue;
}
int tmph;
string str = to_string(node->val) + "_" + to_string(tmpn->val);
if(direc_height.count(str) != ){
tmph = direc_height[str];
}else{
tmph = getHeight(tmpn, node);
direc_height[str] = tmph;
}
h = max(h, tmph);
}
return h + ;
}
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
map<int, gnode*> val_node;
for(int i = ; i < n ; i++){
gnode* node = new gnode(i);
val_node[i] = node;
}
for (int i = ; i < edges.size(); i++){
pair<int, int> pp = edges[i];
val_node[pp.first]->neighbours.push_back(val_node[pp.second]);
val_node[pp.second]->neighbours.push_back(val_node[pp.first]);
}
int minh = INT_MAX;
map<int, int> node_height;
map<int, gnode*>::iterator m_iter;
for(m_iter = val_node.begin(); m_iter != val_node.end(); m_iter++){
int h = getHeight(m_iter->second, NULL);
node_height[m_iter->first] = h;
minh = min(minh, h);
}
vector<int> res;
map<int, int>::iterator mii_iter;
for(mii_iter = node_height.begin(); mii_iter != node_height.end(); mii_iter++){
if(mii_iter->second == minh){
res.push_back(mii_iter->first);
}
}
return res;
}
思路二 :
除了 DP 方案,没有想到其他思路,在网上借鉴了其他了的想法,理解后实现通过。
这个思路实际上是一个 BFS 思路。和常见的从根节点进行 BFS 不同,这里从叶子节点开始进行 BFS。
所有入度(即相连边数)为 1 的节点即是叶子节点。找高度最小的节点,即找离所有叶子节点最远的节点,也即找最中心的节点。
找最中心的节点的思路很简单:
- 每次去掉当前图的所有叶子节点,重复此操作直到只剩下最后的根。
根据这个思路可以回答题目中的 [ hint : How many MHTs can a graph have at most? ],只能有一个或者两个最小高度树树根。证明省略。
class TNode{
public:
int val;
unordered_set<TNode*> neighbours;
TNode(int val){
this->val = val;
}
};
vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
map<int, TNode*> val_node;
for (int i = ; i < n ; i++){
TNode* tmp = new TNode(i);
val_node[i] = tmp;
}
for (int i = ; i < edges.size(); i++){
pair<int, int> pp = edges[i];
val_node[pp.first]->neighbours.insert(val_node[pp.second]);
val_node[pp.second]->neighbours.insert(val_node[pp.first]);
}
map<int, TNode*>::iterator m_iter;
while(val_node.size() > ){
// obtain all leaves in current graph;
list<TNode*> listg;
for ( m_iter = val_node.begin(); m_iter != val_node.end(); m_iter++){
if(m_iter->second->neighbours.size() == ){
listg.push_back(m_iter->second);
}
}
// remove all leaves
list<TNode*>::iterator l_iter;
for(l_iter = listg.begin(); l_iter != listg.end(); l_iter++){
TNode* p = (*(*l_iter)->neighbours.begin());
p->neighbours.erase(*l_iter);
(*l_iter)->neighbours.erase(p);
val_node.erase((*l_iter)->val);
}
}
vector<int> res;
for ( m_iter = val_node.begin(); m_iter != val_node.end(); m_iter++){
res.push_back(m_iter->first);
}
return res;
}
参考资料:
C++ Solution. O(n)-Time, O(n)-Space, LeetCode OJ
[LeetCode] 310. Minimum Height Trees 解题思路的更多相关文章
- 【LeetCode】310. Minimum Height Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 相似题目 参考资料 日期 题目地址:http ...
- [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_Medium tag: BFS
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- 310. Minimum Height Trees
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- 310. Minimum Height Trees -- 找出无向图中以哪些节点为根,树的深度最小
For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...
- [LeetCode] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [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 ...
随机推荐
- iOS多线程编程之GCD的使用
什么是线程呢? 1个CPU执行的CPU命令列为一条无分叉的路径即为线程. 这种无分叉路径不止1条,存在多条时即为多线程. 什么是GCD? Grand Central Dispatch (GCD)是异步 ...
- linux (ubuntu) 下设置 tomcat 随系统自动启动
网上说的有很多, 我只记录一种 1. 切换到 /etc/init.d/ 目录下 2. sudo vim tomcat 3. 在打开的文件里写入以下内容 #!/bin/sh # chkconfig: # ...
- discuz! X3.2 自定义后台门户模块模板里的标签
这里只提供对源码的修改, 至于插件, 暂不考虑... 想在首页里展示一些自定义字段的内容, 奈何dz无此功能, 无奈去扒源码. 首先切到 source 文件夹下 1. 在 class/block/po ...
- jquery对同级的td做radio限制
<html> <head> <title></title> <script src="http://libs.baidu.com/jqu ...
- 命令行命令mvn
在cmd命令行敲击mvn clean install 默认读取的maven配置文件是D:\software\apache-maven-3.2.1\conf\settings.xml 这个文件里面配置有 ...
- 三、T4模板与实体生成
上文我们最后虽然用模板创建了一个实体类,但是类的内容仍旧是静态的,这里我们需要用动态方式生成类的内容.因为需要查询数据库这里又免不了各种繁琐的连接数据库操作,为了使我们的编码更加直观,仍然采 ...
- solr可用于集群的搜索 【转】
一. SOLR搭建企业搜索平台 运行环境: 运行容器:Tomcat6.0.20 Solr版本:apache-solr-1.4.0 分词器:mmseg4j-1.6.2 词库:sogou-dic 准备工 ...
- PHP MySQL 读取数据
PHP MySQL 读取数据 从 MySQL 数据库读取数据 SELECT 语句用于从数据表中读取数据: SELECT column_name(s) FROM table_name 如需学习更多关于 ...
- 五、C# 类
面向对象编程 类是面向对象编程的3个主要特征---封装.继承和多态性---的基础. 封装允许隐藏细节. 继承 继承关系至少涉及两个类,其中一个类(基类)是另一个类的更泛化的版本. 为了从一 ...
- Hibernate 性能优化之二级缓存
二级缓存是一个共享缓存,在二级缓存中存放的数据是共享数据特性 修改不能特别频繁 数据可以公开二级缓存在sessionFactory中,因为sessionFactory本身是线程安全,所 ...