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 / null entries 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