Skip to the code
cf.
+10 XP

#353 Breadcrumb Trails

easy

A site's navigation menu is nested JSON: each entry is { "label": ..., "children": [...] }, and children may be missing or empty. An entry with no children is a page you can open.

Write breadcrumbs(menu) that returns the breadcrumb for every page: the labels from the top entry down to the page, joined with " / ". List them in the order a depth-first walk reaches the pages, visiting children in their given order.

Example 1

Input: menu = {"label":"Home","children":[{"label":"Shop","children":[{"label":"Shoes"},{"label":"Bags"}]},{"label":"About"}]}
Output: ["Home / Shop / Shoes","Home / Shop / Bags","Home / About"]

Shop has two pages under it, and About is a page itself.

Example 2

Input: menu = {"label":"Home"}
Output: ["Home"]

The top entry has no children, so it is the only page.

Constraints

  • 1 <= number of entries <= 5000
  • Labels are non-empty and contain no /.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).