Get all descendant nodes of a given node — the whole subtree, not just
the direct children. Each node appears once; a cycle terminates.
The child is marked visited by the recursive call that opens on it, NOT
by the loop before it. Marking it here and then recursing made
traverse's own visited.has(n.path) guard fire on entry every single
time, so the walk returned after one level: a -> b -> c -> d answered
[b] instead of [b, c, d]. The two tests over this function could not
see it — one asserts only that the result is duplicate-free (true of a
one-level result) and the other's expectation, ['b'] for a two-node
cycle, is what the truncated walk produces anyway.
Get all descendant nodes of a given node — the whole subtree, not just the direct children. Each node appears once; a cycle terminates.
The child is marked visited by the recursive call that opens on it, NOT by the loop before it. Marking it here and then recursing made
traverse's ownvisited.has(n.path)guard fire on entry every single time, so the walk returned after one level:a -> b -> c -> danswered[b]instead of[b, c, d]. The two tests over this function could not see it — one asserts only that the result is duplicate-free (true of a one-level result) and the other's expectation,['b']for a two-node cycle, is what the truncated walk produces anyway.