Invert Binary Tree

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

Invert a binary tree given as a level-order list. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

A tree can be mirrored when a hierarchy must be displayed from the opposite side. The input uses level-order values and None for missing children. Swap the left and right child at every tree node and return the resulting tree in level-order list form. Write invert_tree(level_order: list) -> list. The children 2 and 7 swap, 1 and 3 swap, and 6 and 9 swap, so the sample becomes [4, 7, 2, 9, 6, 3, 1]. The starter file already has the function name and harness print lines. Keep them and fill only the function body.

Requirements

  • Invert every node, not only the root.
  • Preserve values and missing-child positions.
  • Return level-order list form.

Constraints

  • Input uses None for missing children and trailing None values may be omitted.
  • The return type is a list.

Examples

Input: invert_tree([4, 2, 7, 1, 3, 6, 9]) Output: [4, 7, 2, 9, 6, 3, 1] Children swapped at every node.

Topics: lakebench, python, invert, level-order.

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

advanced

Invert Binary Tree

Interview-style drill: Invert a binary tree given as a level-order list.

Implement `invert_tree(level_order: list) -> list`. Input/output use level-order lists with `None` for missing children (trailing Nones may be omitted). Example: `[4, 2, 7, 1, 3, 6, 9]` → `[4, 7, 2, 9, 6, 3, 1]`. Keep the harness.