Skip to content

Binary Trees

Lesson 2 of 14 · 4 min

Binary Trees

Step 1 of 11

A binary node has exactly two child slots: left and right. Never three.

The Idea

A binary tree gives every node exactly two child slots: left and right. Either slot may sit empty, and the two are not interchangeable, so swapping them produces a different tree. That fixed shape is what lets an algorithm say "go left" and mean something precise.

Real-World Example

A TV aerial splitter. One cable feeds in and two run out, and each output can feed another splitter or go straight to a set. Nothing ever merges back together, and the two sockets are physically distinct, not a pair you can swap.

The Code

Your turn

Put the steps in the right order.

  1. Recursively count the left subtree
  2. Return 0 when the node is empty
  3. Add one for this node and return the total
  4. Recursively count the right subtree

Mini quiz

1 / 3

How many children can a binary tree node have?