A ring that grows
transcript
A reader opens a comment. Highlight comments two links away. Up as well as down. A tree has one path between two nodes. Grow a ring.
The ring starts with the id three. Each node has at most three neighbours. Add its two children and its parent. That is one, six and eight.
Each round moves one link further out. One has no children. Six has four. Eight has ten as its other child. The ring is four and ten. Three is seen, so it is skipped. Stop after k rounds, and read the ring. Here k is two, so the ring is the answer.
A parent is a neighbour too. Mark each node when it joins a ring. Then no node is counted twice. Each node joins exactly one ring.
After k rounds, the ring is the answer. Each node joins one ring, so it costs n. Use loops, never deep recursion. Full breakdown in the lecture.