Medium · Trees

Monarchy succession order

Given lines where each line is a parent's name followed by their children's names in birth order (the first line starts with the monarch, and every other person is listed as a child exactly once), return the order of succession: the monarch, then each child's entire line of descent in birth order — a preorder walk of the family tree.

Examples

Example 1

{
  "lines": ["king alice bob", "alice carol dave", "bob erin"]
}

Output: king → alice → carol → dave → bob → erin

Example 2

{
  "lines": ["monarch liz phil", "liz anne"]
}

Output: monarch → liz → anne → phil

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Trees problems