I know there are lots of questions on here dealing with Breadth First Search vs Depth First Search, but I think that my situation is a little different.
I have a rooted tree in which each node may have 0, 1 or 2 children (with the expected number being 1). Given a large number n
, I want to find a path through the tree of length n
.
It seems clear that depth-first should be the best way to do this, but I'm not so sure. The width of the tree is very small, which is normally a reason to use breadth first search. In addition, if I use depth first search, then I could end up following going down into a subtree whose height is very close to n
, but smaller than n
. In that case, I will waste a lot of time traversing a tree that can't possibly give me the answer I want