There is a well known centroid decomposition problem for finding the number of âkâ length path in a tree.
Just a simple modification is needed. Need to use a fast data structure like fenwick tree or segment tree to find the the path less than or equal to âkâ.