
In a Meta mobile app such as Facebook or Instagram, a screen can be represented as a tree of views. Given the root view, return the entire view hierarchy as a list of formatted strings, one per line, using indentation to show parent-child relationships.
Implement a function that traverses the tree in preorder: visit the current view first, then recursively visit its children from left to right. Each output line should contain depth levels of indentation, where each level is two spaces, followed by the view's name.
root, a dictionary representing the root view, or null{"name": string, "children": [view, view, ...]}Example 1
Input:
root = {"name": "Screen", "children": [{"name": "Header", "children": []}, {"name": "Feed", "children": [{"name": "StoryTray", "children": []}]}]}
Output:
["Screen", " Header", " Feed", " StoryTray"]
Explanation: Header and Feed are children of Screen, and StoryTray is a child of Feed.
Example 2
Input:
root = null
Output:
[]
Explanation: An empty hierarchy produces no lines.
0 <= number of views <= 10^50 <= number of children per view <= 10^41 <= len(name) <= 100root = {"name": "Screen", "children": [{"name": "Header", "children": []}, {"name": "Feed", "children": [{"name": "StoryTray", "children": []}]}]}Output["Screen", " Header", " Feed", " StoryTray"]WhyThe traversal visits `Screen`, then `Header`, then `Feed`, then `StoryTray`, adding indentation based on depth.root = nullOutput[]WhyThere are no views to print, so the result is empty.root = {"name": "Root", "children": [{"name": "A", "children": [{"name": "B", "children": []}]}]}Output["Root", " A", " B"]WhyEach deeper level adds two spaces, so `B` is indented twice.0 <= number of views <= 10^50 <= number of children per view <= 10^41 <= len(name) <= 100The input forms a valid tree with no cyclesdef print_view_hierarchy(root):