Mirror the Folder Tree
Easy · Trees & BSTs · about 10 min
A file browser shows a folder tree where each folder has at most two subfolders. A user switches to a right-to-left language, so the whole tree must be shown mirrored: at every folder, the left and right subfolders swap places. The tree is a level-order list: the root first, then each node's left and right child in turn, with None (Python) / null (JavaScript) for a missing child. A build(level) helper in the starter turns it into nodes with .val, .left and .right.
Return the mirrored tree as a level-order list in the same format, with any None / null at the very end removed. A to_level(root) helper in the starter does that conversion for you.
- Up to 10,000 folders. Values are folder ids (any integers).
Examples:
mirror_folders([4, 2, 7, 1, 3, 6, 9]) → [4, 7, 2, 9, 6, 3, 1]
mirror_folders([1, 2]) → [1, None, 2]
Hints
- If you could mirror the left subtree and the right subtree, what is left to do at the current node?
- The empty tree mirrored is the empty tree.
- Make sure the swap reaches every node, both sides, all the way down.
Learn the pattern first: the Trees & BSTs lesson.
More Trees & BSTs problems
- Levels in the Org Chart (Easy)
- Twin Backup Check (Easy)
- Exact Budget Trail (Easy)
- Shared Boss in the ID Tree (Easy)
- Floor-by-Floor Roster (Medium)
- Unbeaten on the Way Down (Medium)
- Longest Handoff Chain (Medium)
- Shelf Index Check (Medium)
- K-th Cheapest Price (Medium)
- Closest Shared Folder (Medium)
- Rebuild the Bracket (Medium)
- Best Relay Route (Hard)
- Downhill Trails With a Target Sum (Hard)
- Office Wi-Fi Repeaters (Hard)