Given a tree with n nodes, Print all nodes which are the endpoint of the diameter. 1<= n <=100000.
For Above tree answer would be A,B,F,G
I tried the O(n2) approach (Running Dfs from each node) but need a solution better than it.
Given a tree with n nodes, Print all nodes which are the endpoint of the diameter. 1<= n <=100000.
For Above tree answer would be A,B,F,G
I tried the O(n2) approach (Running Dfs from each node) but need a solution better than it.
One-line takeaway: a tree has either one center or two adjacent centers, which are shared by all diameters.
The clue to solve the problem faster is sort of hidden in the symmetric tree given in the question. You can observe that the node D is the center of the tree. Every endpoint of a diameter is 2 edges away from D.
However, all trees are not symmetric. For example, we can remove node B from the given graph. Well, node D remains to be "the center".
In general, if a diameter of a tree is of even length, that diameter has a node at its center. For example, the diameter
$\quad\quad$ A - C - D - E - G $\quad\quad$
has node D as its center node. Moreover, that center node is the center node of every diameter of the tree. So in this case, we can locate the center. Then breadth-first (level-by-level) search starting from the center. The nodes we will visit at the furthest level will be all endpoints of all diameters.
Another case is when diameters are of odd length. Then each diameter has two nodes in the middle. For example, imagine node F and G are removed. Then node C and D are the center nodes. Moreover, those two center nodes are the centers nodes of every diameter of the tree. In this case, we will locate the centers. Imagine the edge between the two centers is broken. Then we can do two breadth-first (level-by-level) search, each time starting from one of the two centers. The nodes we will visit at the furthest level in either BFS will be all endpoints of all diameters.
Let $n$ be the number of nodes in the tree.
So, the whole algorithm takes $O(n)$ time.
Exercise. Show that all diameters of a tree share their centers, whether there is one center or two centers.