
In a Meta mobile app such as Facebook or Instagram, UI debugging often requires inspecting the full view tree from a root view. Given the root of a view hierarchy, return a formatted list of strings representing the entire hierarchy in top-down order, with indentation showing parent-child relationships.
Each view has a name string and a list of children. Write a function that traverses the hierarchy and returns one string per view. The root should have no indentation, its children should be prefixed with two spaces, grandchildren with four spaces, and so on.
root, a view node object or Nonename: stringchildren: list of child view nodesExample 1
Input:
root = View("Root", [View("Header"), View("Body")])
Output:
["Root", " Header", " Body"]
Explanation: Header and Body are direct children of Root, so each is indented by two spaces.
Example 2
Input:
root = View("Root", [View("Feed", [View("StoryTray"), View("Composer")])])
Output:
["Root", " Feed", " StoryTray", " Composer"]
Explanation: StoryTray and Composer are grandchildren of Root, so they use four spaces.
[0, 10^4][1, 100]0 to 100 childrenroot = View("Root", [View("Header"), View("Body")])Output["Root", " Header", " Body"]WhyBoth `Header` and `Body` are direct children of the root, so each gets one indentation level.root = View("Root", [View("Feed", [View("StoryTray"), View("Composer")])])Output["Root", " Feed", " StoryTray", " Composer"]WhyThe traversal prints `Root`, then `Feed`, then its children. Grandchildren are indented by four spaces.The number of views is in the range [0, 10^4]1 <= len(view.name) <= 100Each node has 0 to 100 childrenThe input hierarchy is a tree with no cyclesdef print_view_hierarchy(root):