Coding: Finding the Diameter of a Tree

In SRM 635, a problem (LonglongestPathTree) was set for division 2 hard which requires to find the diameter of a tree.

Finding Diameter of tree

To find the diameter of a tree, at first a dfs/bfs search is needed from any arbitrary node(for say ‘x’) to find a node(y) which is furthest from x with respect to cost. From node y, a second search is required to find the most expensive cost path in the tree.

In this specific problem given in Topcoder, you have an option to increase the diameter of the tree by deleting…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store