0
$\begingroup$

I was going through this article https://www.geeksforgeeks.org/count-nodes-within-k-distance-from-all-nodes-in-a-set/

The question says: Given an undirected tree with some marked nodes and a positive number K. We need to print the count of all such nodes which have distance from all marked nodes less than K which means every node whose distance from all marked nodes is less than K, should be counted in the result.

.The solution mentioned says we find two marked nodes that are at a max distance among all the pairs of marked nodes, and any node which is within k distance from both the nodes will be within k distance from all the marked nodes, and for that they do bfs from "random node" to get first distant marked node and then the second bfs from this found node to get the second distant node. I understood how the second bfs from the first distant node finds other distant node but I am unable to visualize how could a bfs from "random node" in the tree helps to find one of the distant nodes.

$\endgroup$