How trees are written on these pages

Trees Junior Python 3.13+ Reviewed By Best Latest Apps

Predict the output

Using the list form above, what does this print?

Python 3.13
level = [8, 3, 10, 1, 6, None, 14]
i = level.index(10)
kids = [level[j] for j in (2 * i + 1, 2 * i + 2) if j < len(level)]
print(i, kids)

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A 2 [None, 14]

Right: 10 sits at position 2, so its children are at 5 and 6.

Children of position i are at 2i + 1 and 2i + 2, and None marks the missing left child of 10.

Why not the others?
  • B 2 [1, 6]. Positions 3 and 4 hold the children of 3, at position 1.
  • C 1 [None, 14]. list.index finds 10 at position 2, not 1.

30-second answer

Every tree page uses one small dataclass: a value and two optional children. Test cases write a tree as a list in level order, where the children of position i sit at 2i + 1 and 2i + 2 and None marks a missing node. A helper turns that list into Nodes before the solution runs.

Practise saying it · 30 s. No recording, just a timer

Describe the node class and test format you would use.

Say your answer out loud, then check which of these points you covered:

  • Value plus two optional children
  • Level-order list with None
  • Agree on format early

Deep dive

The list form is compact for tests and matches how heaps are stored; interviewers usually just hand you a root node.

Missing nodes still take a slot in this list form, so a chain-shaped tree needs a long list; tests keep trees small.

In an interview, define the node class in two lines if it is not given, then move on.

The Node class and the list helper Ran on Python 3.13.5 · mypy --strict
Python 3.13
from dataclasses import dataclass


@dataclass
class Node:
    val: int
    left: "Node | None" = None  # smaller side in a search tree
    right: "Node | None" = None  # larger side in a search tree


def build(level: list[int | None]) -> Node | None:
    nodes = [None if v is None else Node(v) for v in level]
    for i, node in enumerate(nodes):
        if node is not None and 2 * i + 1 < len(nodes):
            node.left = nodes[2 * i + 1]
        if node is not None and 2 * i + 2 < len(nodes):
            node.right = nodes[2 * i + 2]
    return nodes[0] if nodes else None


root = build([8, 3, 10, 1, 6, None, 14])
assert root and root.right and root.right.right
print(root.val, root.right.right.val)

Common mistakes

  • Assuming every interviewer uses the same list format; ask or define it.
  • Mutating shared nodes in one test and reusing them in the next.

Senior take & follow-ups

Spend ten seconds agreeing on the node shape and input format. It prevents ten minutes of confusion.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why a dataclass rather than a dict?

    Show hint

    Attribute access is clearer, type checkers understand it, and equality comes free.

  2. How is this different from a heap?

    Show hint

    A heap keeps the list form for real and adds an ordering rule; here the list only describes the shape.

What each operation costs: Binary Tree · Binary Search Tree · Tree Traversal

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.