Define Maximum Distance of a node to be the maximum of the distances between that node and all other nodes in the tree. My problem is to find and print the maximum distance for all nodes within a tree (not necessarily binary or anything). Basically, for each node, I need to print out the maximum distance that node has between the node we are looking at, and any other node within the tree. The runtime is expected to be O(n).
My best approaches all take O(N^2) time and I'm not sure where else to go with this problem. I currently run BFS on every node in the tree to find the max distance for each node in the tree, but I think a better approach might be to use some form of dynamic programming. I'm not sure though.
Thanks for any help in advance.