- You start from a leaf node (a node with degree 1).
- At each step:
- Move to the next connected vertex.
- Only "read" (output) a vertex if its degree is 1, or if its degree is less than the previous vertex’s degree.
- If not, pass through it and explore its adjacent vertices.
- Build the tree from input edges.
- Find a leaf node (any node with degree 1).
- Traverse the tree from this leaf node with the custom logic:
- Keep track of the degree of the previous node.
- If current node degree < previous node degree => read it.
- If current node degree == 1 => read it.
- Else => keep traversing until above condition is met.