A tree is a list that grew branches: one root on top, exactly one path down to every node, and a whole new set of ways to visit the data.
1 Hierarchy, not a line
Lists and stacks are linear. Trees branch — one root, and every other node has exactly one parent above it.
2 Binary = at most two
Each node keeps up to two children, named left and right. That single rule is all a binary tree needs.
3 Three DFS orders
Depth-first walks come in three flavours: inorder (left, node, right), preorder (node, left, right), postorder (left, right, node).
4 BFS rides a queue
Level-order visits top to bottom, left to right — a deque holds the waiting line of nodes.
5 Height and depth
Depth counts edges from the root down to a node; height is the longest root-to-leaf path. Our 7-node tree is 2 edges tall.
6 Leaves and None
A leaf has zero children. In Python, missing children are None — the base case every tree recursion stops at.
Meet the trees
Hierarchy, not a line1Lists and stacks are linear. Trees branch — one root, and every other node has exactly one parent above it. Binary = at most two2Each node keeps up to two children, named left and right. That single rule is all a binary tree needs. Three DFS orders3Depth-first walks come in three flavours: inorder (left, node, right), preorder (node, left, right), postorder (left, right, node). BFS rides a queue4Level-order visits top to bottom, left to right — a deque holds the waiting line of nodes. Height and depth5Depth counts edges from the root down to a node; height is the longest root-to-leaf path. Our 7-node tree is 2 edges tall.
Leaves and None6A leaf has zero children. In Python, missing children are None — the base case every tree recursion stops at. 6 building blocks make up Meet the trees
Left subtree, then the node, then right subtree — recursion visits all seven nodes in a strict order.
1 / 10Our tree: 41 on top, 65 to its left, 20 to its right, four leaves below. Inorder = left subtree, node, right subtree. The walk starts at the root.
Preorder with an explicit stack: pop a node, visit it, push right then left — so the left child pops first.
process decision input / output start / end The same three-node tree twice: recursion for inorder, an explicit list-stack for preorder.
moduleroot = TreeNode(41) — the root is born holding 41, with both child links empty.
Memory
root= val=41, left=None, right=None
Console
output appears as the program runs…
Same tree, new rule: a queue drains it level by level — parents first, then all four leaves.
1 / 8Same tree, new rule: level-order sweeps top to bottom, left to right. A queue holds the waiting line — it starts as [41].
Recursion measures the tree: height counts the longest root-to-leaf path in nodes.
Your predicted output — one line per console line
Hint: height returns 0 for None, so a leaf counts 1. The deepest path here is 7 → 9 → 12 — three nodes. And t.r.r walks right twice: 9, then 12.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?rootdrops into place
?leafdrops into place
?inorderdrops into place
?preorderdrops into place
?postorderdrops into place
?level-orderdrops into place
?heightdrops into place
?Nonedrops into place
01A node = value + left link + right link; the root has no parent, leaves have no children.
02Missing children are None — the base case every tree recursion stops at.
03Inorder: left, node, right. Preorder: node, left, right. Postorder: left, right, node.
04DFS uses a stack (the call stack or an explicit list); level-order BFS uses a queue.
05Same tree, different walks: 50, 65, 91, 41, 32, 20, 74 inorder vs 41, 65, 20, 50, 91, 32, 74 by level.
06Trees model hierarchy — file systems, the DOM, org charts and autocomplete all live in trees.