现在的位置: 首页 > 综合 > 正文

求一个树的最长路径

2017年05月14日 ⁄ 综合 ⁄ 共 276字 ⁄ 字号 评论关闭

树的最长路径指的是树上两个节点的最大距离。

Algorithm:

  1. Run BFS or DFS from any node to find the farthest leaf node. Label that node T.
  2. Run another BFS or DFS to find the farthest node from T.
  3. The path you found in step 2 is the longest path in the tree.

转载http://www.quora.com/How-does-following-algorithm-for-finding-longest-path-in-tree-work

抱歉!评论已关闭.