Answer the question
In order to leave comments, you need to log in
How to find the distance between vertices in a tree?
Friends, hello! I can't guess, how can I find out the distances between two entered vertices in the tree using DFS? (The tree is undirected and unweighted)
For example from the picture:
The distance between 0 and 15 is three.
Answer the question
In order to leave comments, you need to log in
For such business BFS is more convenient.
When adding nodes to the queue, just add the distance to the same place (if the current processed node has a distance of N, then the children in the queue get N + 1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question