树直径、二叉树直径 Tree diameter (Longest path in an undirected tree)
来源:互联网 发布:易语言mp3播放器源码 编辑:程序博客网 时间:2024/06/05 17:09
树直径问题其实就好像在一个无向无环图中找一条最长的路径,核心思想其实也很简单:
首先从任意一个节点开始BFS找距离最远能到达的点(以该点作为根节点树的最深层),假设到达点为V,再以V为根节点找最远能到达的点(即以V为根节点树的最深层),两次BFS即可解决。
按照上述方法,其实最开始任意选的那个点就是最长路径中的一个节点而已,这个节点可能是最长路径的端点也有可能是路径中的一点。
代码:
#include <bits/stdc++.h> using namespace std;class Graph { int V; list<int> *adj;public:Graph(int v) : V(v){adj = new list<int>[V];}void addEdge(int u, int v);void longestPathLength();pair<int, int >BFS(int u);}; void Graph::addEdge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); }pair<int, int> Graph::BFS(int u) {int dis[V];fill(dis, dis + V, -1);queue<int> q;q.push(u);dis[u] = 0;while(!q.empty()) { int t = q.front();q.pop();for(auto it = adj[t].begin(); it != adj[t].end(); ++it) { if(dis[*it] == -1) { q.push(*it); dis[*it] = dis[t] + 1; }} } int maxDis = 0; int idx = -1; for(int i = 0; i < V; ++i) { if(dis[i] > maxDis) { idx = i; maxDis = dis[i]; } } return make_pair(idx, maxDis);}void Graph::longestPathLength() { pair<int, int> t1, t2; t1 = BFS(0); t2 = BFS(t1.first); cout << "Longest path is from " << t1.first << " to " << t2.first << " of length " << t2.second << endl;}int main(){ ios::sync_with_stdio(false); Graph g(10); g.addEdge(0, 1); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(2, 9); g.addEdge(2, 4); g.addEdge(4, 5); g.addEdge(1, 6); g.addEdge(6, 7); g.addEdge(6, 8); g.longestPathLength(); return 0; }
对于求二叉树的直径其实更简单,取根节点的左右孩子树高之和加一和左右孩子中的直径三者中的最大值(直径可能不包含根节点),同理孩子节点递归向下求,核心代码:
int Diameter(Node *root, int *hegiht) { if(root == NULL) { *hegiht = 0; return 0; } int lh = 0, rh = 0; int ldiameter = 0, rdiameter = 0; ldiameter = Diameter(root->left, &lh); rdiameter = Diameter(root->right, &rh); *height = max(lh, rh) + 1; // recording height of tree return max(lh + rh + 1, max(ldiameter, rdiameter)); }
阅读全文
0 0
- 树直径、二叉树直径 Tree diameter (Longest path in an undirected tree)
- 543. Diameter of Binary Tree (二叉树的直径)
- 【二叉树】树的直径【543. Diameter of Binary Tree】
- 543. Diameter of Binary Tree 二叉树的直径
- 543. Diameter of Binary Tree | 二叉树的“直径”
- 543. Diameter of Binary Tree 二叉树的直径
- leetcode解题之543. Diameter of Binary Tree Java版 (二叉树的最大直径)
- 【SPOJ-PT07Z】Longest path in a tree【DFS/BFS】【树的直径】
- [LeetCode]543. Diameter of Binary Tree(计算二叉树的直径的长度)
- CF804D:Expected diameter of a tree(树的直径 & dfs)
- Codeforces 804D Expected diameter of a tree(树的直径)
- LightOJ1094Farthest Nodes in a Tree(BFS+树的直径)
- Farthest Nodes in a Tree(树的直径)
- Farthest Nodes in a Tree(树的直径)
- Codeforces Round #411 (Div. 1) D. Expected diameter of a tree(树的直径)
- [树的直径] Codeforces 804D Round #411 (Div. 1) D. Expected diameter of a tree
- LightOJ 1094 - Farthest Nodes in a Tree【树的直径】
- LightOJ - 1094 Farthest Nodes in a Tree(树的直径)
- 极其简单的 使用IDEA 中 实现springboot 热部署 (spring boot devtools版)
- WebApi 接口参数不再困惑:传参详解
- Highway Networks
- 将testng测试结果存储到数据库中
- solidworks安装出现1903报错处理办法
- 树直径、二叉树直径 Tree diameter (Longest path in an undirected tree)
- 关于微信小程序的scroll-view的使用
- webp格式图片转化为常见的png格式图片
- mui jquery 同时使用
- apache
- Win32 串口编程(三)
- 对jQuery的理解
- 动态规划
- windows7 环境下配置python开发环境