Twin Backup Check
Easy · Trees & BSTs · about 10 min
A backup tool stores a folder tree twice and wants to know if the two copies are identical: the same shape, with the same value in every matching position. 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 True / true if the two trees are identical, otherwise False / false.
- The two lists may be written differently and still describe the same tree (for example one has extra
None/nullentries at the end). - Up to 10,000 nodes each. Values are any integers.
Examples:
same_backup([1, 2, 3], [1, 2, 3]) → True
same_backup([1, 2], [1, None, 2]) → False (2 is a left child in one copy and a right child in the other)
Hints
- If one tree is empty, when are they the same?
- When both are non-empty, what three things must all hold?
- Compare the structure, not the raw lists: the same tree can be written in more than one way.
Learn the pattern first: the Trees & BSTs lesson.
More Trees & BSTs problems
- Levels in the Org Chart (Easy)
- Mirror the Folder Tree (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)