Same Tree

Python data engineering interview problem. Difficulty: advanced. Pattern: Trees. About 18 minutes. Part of the Pro drill bank.

Check whether 2 level-order trees are identical. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

2 versions of a hierarchy should be compared by both their values and their shape. A missing child is different from a real child whose value happens to be similar. Return whether the 2 level-order tree inputs represent the same tree. Compare corresponding nodes and their child structure. Write is_same_tree(a: list, b: list) -> bool. [1, 2, 3] and [1, 2, 3] have matching values and shape, so they are the same. The starter file already has the function name and harness print lines. Keep them and fill only the function body.

Requirements

  • Compare every corresponding value.
  • Treat missing children as structural information.
  • Return one boolean.

Constraints

  • Trees use level-order lists and None for missing nodes.
  • The return type is bool.

Examples

Input: is_same_tree([1, 2, 3], [1, 2, 3]) Output: True Identical structure and values.

Topics: lakebench, python, compare, dfs.

More Python interview questions · All interview problems · Learn data engineering

advanced

Same Tree

Interview-style drill: Check whether 2 level-order trees are identical.

Implement `is_same_tree(a: list, b: list) -> bool`. Example: `[1, 2, 3], [1, 2, 3]` → `True`. Keep the harness.