3.4.2 · D1Trees

Foundations — Binary tree — structure, traversals - preorder, inorder, postorder (recursive and iterative)

2,163 words10 min readBack to topic

This page assumes you have seen nothing. Before you can read the parent note comfortably, every word, arrow, and symbol it uses must first mean something concrete to you. We build them in an order where each one leans only on the ones before it.


1. A "node" — the value-box

Picture a labelled box. Inside it: the value. Sticking out of it: up to two arrows, one pointing down-left, one pointing down-right. If an arrow points to nothing, we draw it as ✗ (empty).

Figure — Binary tree — structure, traversals -  preorder, inorder, postorder (recursive and iterative)

Why the topic needs this. The entire chapter is "visit every node." If you do not have a crisp mental image of one node — a value plus two outgoing arrows — the phrases node.left and node.right in the code are meaningless. Everything is built out of this one brick.

In code the box is:

class Node:
    def __init__(self, val):
        self.val = val     # the value inside the box
        self.left = None   # the down-left arrow (points to a Node or to nothing)
        self.right = None  # the down-right arrow

2. None / null — the "arrow points to nothing"

Picture the ✗ from the previous figure. That ✗ is None. It is not a box with the value zero; it is the absence of a box.

Why the topic needs this. None is the stopping signal for everything recursive. The very first line of every traversal is if node is None: return. If you think None means "a box holding nothing," you will try to read None.left and crash. None means no box at all.


3. Root, parent, child, leaf — family words

Once boxes point to boxes, we get a family. The words below are just names for positions in that family.

Figure — Binary tree — structure, traversals -  preorder, inorder, postorder (recursive and iterative)

Why the topic needs this. The traversal orders are described entirely in these words: "visit the parent before its children" (preorder), "between its children" (inorder), "after its children" (postorder). Without knowing which box is parent vs child, the sentence has no meaning.


4. "Subtree" — a child is itself a whole tree

This is the single most important idea, so it gets its own figure.

Figure — Binary tree — structure, traversals -  preorder, inorder, postorder (recursive and iterative)

Look at the figure. The whole thing is a tree with root 1. But draw a loop around box 2 and everything below it — that loop contains a perfectly good little tree with root 2. That is the left subtree of 1. The single box 3 (with two None arrows) is the right subtree of 1, also a valid tree.

Why the topic needs this — this is the heart of everything. Because a child's subtree is the same kind of thing as the whole tree, the exact same procedure works on it. This is called self-similarity. It is why recursion (next section) fits trees perfectly: "traverse the tree" = "visit root, then traverse the left subtree the same way, then traverse the right subtree the same way." The problem shrinks but never changes shape.


5. Recursion — a rule that calls itself

Two mandatory parts:

  1. Base case — the smallest input, answered with no further calls. For trees this is if node is None: return (an empty tree needs zero work — you are done).
  2. Recursive step — reduce to smaller sub-problems (the left subtree and the right subtree) and call yourself on each.

See Recursion and the Call Stack for the machinery. The key visual: each call waits for its inner calls, so calls nest like Russian dolls.


6. The call stack — how recursion remembers

Figure — Binary tree — structure, traversals -  preorder, inorder, postorder (recursive and iterative)

The stack grows downward into the tree (one saved frame per level you descend) and shrinks as you climb back up. The figure shows the stack holding exactly the chain of ancestors from the root to where you currently are — never more.

Why the topic needs this. Two reasons:

  • It explains the space cost (see §8): at any instant the stack holds only one box per level, so its maximum height equals the tree's height .
  • The iterative traversals in the parent note replace this hidden stack with an explicit Stack (LIFO) you build yourself. To understand why iterative preorder pushes the right child before the left, you must first see that a stack is LIFO — Last In, First Out — the last thing pushed is the first popped.

7. Big-O notation — measuring cost

Symbols you will meet:

Why the topic needs this. The parent states Time (each box is touched a constant number of times) and Space (stack depth). Those statements are unreadable unless , , , and each already mean something.


8. Balanced vs skewed — the two extremes of

This is exactly why the parent says balanced traversal uses stack space but a skewed tree uses . Same algorithm, cost set by the shape. More in Tree Height and Balance.


How these feed the topic

Node = value plus two arrows

None = arrow to nothing

Family words root parent child leaf

Subtree = child is a whole tree

Self similarity

Recursion base case plus recurse

Call stack remembers ancestors

Explicit Stack LIFO for iterative

Space cost O of h

n and h and log

Balanced vs skewed

Three traversals Pre In Post

Everything on the left must be solid before the three traversals on the right make sense. The two paths — recursion (hidden stack) and iteration (explicit Stack (LIFO)) — meet at the same result.


Where these lead next

  • Binary Search Tree — a tree where left < node < right, so inorder comes out sorted.
  • Level-order Traversal (BFS) — visiting level by level using a queue instead of a stack.
  • Construct Tree from Preorder + Inorder — rebuilding the boxes from two traversal lists.

Equipment checklist

Cover the answers. If any line stumps you, re-read its section before the parent note.

What is a node made of?
One value plus two arrows (left, right), each pointing to a child Node or to None.
What does None mean and what does it NOT mean?
It means "no box here" (the stopping signal); it does not mean a box holding the value 0.
Which box is the root?
The single top box that no arrow points to — it has no parent.
What is a leaf?
A box whose both arrows are None (no children).
What is a subtree, and why does it matter?
Any box plus everything below it — itself a valid smaller tree; this self-similarity is why recursion works.
What are the two mandatory parts of any recursion?
A base case (if node is None: return) and a recursive step (call yourself on the smaller subtrees).
Why use recursion instead of a plain loop for trees?
A tree branches two ways at each box; recursion follows both and automatically remembers where to return.
What does the call stack hold at any instant during traversal?
The chain of ancestor boxes from the root down to the current box — at most one per level.
What does LIFO mean and why does it force "push right before left"?
Last In First Out; the last pushed pops first, so to process left first you must push it last (after right).
What do and stand for?
= number of nodes; = height = number of levels (longest root-to-leaf path).
Why is space and not ?
The stack only holds one box per level (the ancestor chain), so its depth is the height, not the whole count.
When does and when does ?
Skewed (one-child) tree → ; perfectly balanced tree → .