Predict the output
Two different shapes, same in-order values. What does this print?
def inorder(level: list[int | None], i: int = 0) -> list[int]:
if i >= len(level) or level[i] is None:
return []
v = level[i]
assert v is not None
return inorder(level, 2 * i + 1) + [v] + inorder(level, 2 * i + 2)
a: list[int | None] = [2, 1]
b: list[int | None] = [1, None, 2]
print(inorder(a) == inorder(b), a == b)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A True False
Right: both walk as [1, 2], but the shapes differ.
One traversal order cannot tell these shapes apart, which is why the check compares structure and values together.
Why not the others?
- B True True. The lists describing the trees are different.
- C False False. Both in-order walks give [1, 2].