Predict the output
What does this print?
def h(level: list[int | None], i: int = 0) -> int:
if i >= len(level) or level[i] is None:
return 0
return 1 + max(h(level, 2 * i + 1), h(level, 2 * i + 2))
level = [1, 2, 2, 3, None, None, None, 4]
print(h(level, 1), h(level, 2))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 3 1
Right: the left side goes 2, 3, 4; the right side is a single node.
A gap of 2 between the two sides means the root is out of balance, even though no node has more than one child below it on the right.
Why not the others?
- B 2 1. Node 4 at position 7 is the left child of 3, adding one more level.
- C 3 3. The right child 2 has no children here.