Efficient algorithm to find all distances to a specific node in a tree
Assuming your edges are not directed (which seems to be the case in case you are dealing with trees), you can calculate the distance by starting a breadth-first search from node 2. Then, you get the number of steps necessary to go from node 2 to each node $n$, which, as the graph is undirected, is the same as the number of steps necessary to go from node $n$ to 2.

If your had a directed graph, you can basically do the same, but you have to traverse edges in the opposite direction. E.g. if you are at node $n_1$, and have the edges $n_1 \to n_2$ and $n_3 \to n_1$, you would then process $n_3$ as a successor, but not $n_2$.