Longest path in the tree

Revision en2, by iamdumb, 2015-06-27 22:41:53

Hello everyone,I was trying to do this question from spoj.First I had no idea how to do this question so,I googled and algorithms that came out was this

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

But my problem is that it is becoming really very difficult to implement what is asked.Can anybody show me the code what is asked.Preferably in C++ and using dfs.Thanks.

Tags longest path, c++

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English iamdumb 2015-06-27 22:41:53 39
en1 English iamdumb 2015-06-27 22:41:14 556 Initial revision (published)